Home  >  Article  >  Web Front-end  >  ECharts custom theme: how to create your own chart style

ECharts custom theme: how to create your own chart style

PHPz
PHPzOriginal
2023-12-17 09:10:55974browse

ECharts custom theme: how to create your own chart style

ECharts custom theme: how to create your own chart style

Text:

ECharts (Enterprise Charts) is an open source software based on Baidu JavaScript's visual chart library allows developers to quickly build a variety of beautiful, interactive data visualization interfaces by providing rich chart types and interactive functions. However, the default theme style of ECharts does not necessarily meet our needs. This article will introduce how to customize the theme in ECharts to create your own chart style.

In ECharts, theme refers to the overall style of the chart, including color, font, background and other elements. ECharts provides a wealth of theme styles for developers to use, but sometimes we need to customize unique theme styles according to project needs. The following will use a specific example to demonstrate how to customize the theme.

First, we need to prepare a JS file to store our customized theme style. Taking the histogram as an example, create a file named myTheme.js and define our theme style in the file:

var myTheme = {
    color: ['#4E79A7', '#F28E2B', '#E15759', '#76B7B2', '#59A14F', '#EDC949',
    '#AF7AA1', '#FF9DA7', '#9C755F', '#BAB0AC'],
    
    backgroundColor: '#F7F7F7',

    textStyle: {
        fontFamily: 'Arial, Verdana, sans-serif',
        fontSize: 14,
        fontWeight: 'normal',
        color: '#333333'
    },

    title: {
        textStyle: {
            fontSize: 18,
            fontWeight: 'bold',
            color: '#333333'
        }
    },

    xAxis: {
        axisLine: {
            lineStyle: {
                color: '#999999'
            }
        },
        axisLabel: {
            textStyle: {
                color: '#666666'
            }
        }
    },

    yAxis: {
        axisLine: {
            lineStyle: {
                color: '#999999'
            }
        },
        axisLabel: {
            textStyle: {
                color: '#666666'
            }
        }
    },

    series: {
        itemStyle: {
            borderColor: '#ffffff'
        },
        label: {
            textStyle: {
                color: '#333333'
            }
        }
    }
};

module.exports = myTheme;

In this code, we define some common theme styles, such as color , background color, font style, etc. By modifying these styles, you can personalize the chart.

Next, introduce the theme file we defined into the project. Add the following code to the

tag in the HTML page:
<script src="myTheme.js"></script>

In this way, we have successfully introduced the custom theme style. Next, we can use this custom theme in the initialization code of ECharts.

var chart = echarts.init(document.getElementById('chart_div'), 'myTheme');

// 然后按照常规的方式定义图表的配置项和数据,进行图表的渲染,例如:
var option = {
    title: {text: '柱状图'},
    xAxis: {data: ['A', 'B', 'C', 'D', 'E']},
    yAxis: {},
    series: [{
        name: '销量',
        type: 'bar',
        data: [5, 20, 36, 10, 15]
    }]
};

chart.setOption(option);

In the above code, we specify the name of the custom theme 'myTheme' through the echarts.init() method, then define the configuration items and data of the chart in the usual way, and finally use chart.setOption( ) method applies configuration items to the chart.

After the above steps, we successfully customized the theme style in ECharts and applied it to the bar chart. By modifying the style attributes in myTheme.js, we can personalize the chart style.

Conclusion:

This article introduces how to customize the theme style in ECharts, and demonstrates how to customize the theme style of the histogram through a specific example. By customizing theme styles, we can meet the needs of different projects and create our own chart styles. Of course, in addition to the styles introduced in this article, ECharts also provides more theme style configuration options, which developers can freely combine and customize according to their needs.

Whether it is various chart types such as dashboards, line charts, or maps, ECharts supports theme style customization and provides a rich theme style library. As long as you master the method of customizing themes, you can create unique and beautiful chart effects, making the data more vivid and intuitively presented to users. I hope this article can help everyone understand the theme style customization of ECharts.

The above is the detailed content of ECharts custom theme: how to create your own chart style. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn