Okay, here are the JSON templates, TypeScript interfaces, and explanations for Bar, Line, Scatter, and Pie charts, focusing on a standardized structure, multi-series support, and data binding points.
Key Considerations:
- Standardization: The aim is to create a consistent structure across chart types, making data transformation easier. This includes consistent naming conventions (e.g.,
seriesName, xAxisData, yAxisData).
- Multi-Series: The
series array is the core of handling multiple data series. We'll highlight which chart types benefit most from this.
- Data Binding Points: We'll use placeholder values (e.g.,
"seriesDataPlaceholder") to clearly mark where your actual data needs to be injected.
- TypeScript: The interfaces will ensure type safety and provide IDE support when working with these structures.
1. Bar Chart
- Supports Multiple Series: YES. Bar charts are frequently used to compare multiple categories or groups of data.
{
"title": {
"text": "Bar Chart Title",
"left": "center"
},
"tooltip": {
"trigger": "axis",
"axisPointer": {
"type": "shadow"
}
},
"legend": {
"data": ["Series 1", "Series 2"]
},
"grid": {
"left": "3%",
"right": "4%",
"bottom": "3%",
"containLabel": true
},
"xAxis": {
"type": "category",
"data": ["Category A", "Category B", "Category C"]
},
"yAxis": {
"type": "value"
},
"series": [
{
"name": "Series 1",
"type": "bar",
"data": ["seriesDataPlaceholder1", "seriesDataPlaceholder2", "seriesDataPlaceholder3"]
},
{
"name": "Series 2",
"type": "bar",
"data": ["seriesDataPlaceholder4", "seriesDataPlaceholder5", "seriesDataPlaceholder6"]
}
]
}
interface BarChartSeriesItem {
name: string;
type: 'bar';
data: (string | number)[];
}
interface BarChartOption {
title: {
text: string;
left: 'center' | 'left' | 'right';
};
tooltip: {
trigger: 'axis' | 'item' | 'none';
axisPointer?: {
type: 'line' | 'shadow' | 'cross' | 'none';
};
};
legend: {
data: string[];
};
grid: {
left: string;
right: string;
bottom: string;
containLabel: boolean;
};
xAxis: {
type: 'category';
data: string[];
};
yAxis: {
type: 'value';
};
series: BarChartSeriesItem[];
}
2. Line Chart
- Supports Multiple Series: YES. Line charts are excellent for showing trends across multiple datasets over time or other continuous variables.
{
"title": {
"text": "Line Chart Title",
"left": "center"
},
"tooltip": {
"trigger": "axis"
},
"legend": {
"data": ["Series 1", "Series 2"]
},
"grid": {
"left": "3%",
"right": "4%",
"bottom": "3%",
"containLabel": true
},
"xAxis": {
"type": "category",
"boundaryGap": false,
"data": ["Category A", "Category B", "Category C"]
},
"yAxis": {
"type": "value"
},
"series": [
{
"name": "Series 1",
"type": "line",
"data": ["seriesDataPlaceholder1", "seriesDataPlaceholder2", "seriesDataPlaceholder3"]
},
{
"name": "Series 2",
"type": "line",
"data": ["seriesDataPlaceholder4", "seriesDataPlaceholder5", "seriesDataPlaceholder6"]
}
]
}
interface LineChartSeriesItem {
name: string;
type: 'line';
data: (string | number)[];
}
interface LineChartOption {
title: {
text: string;
left: 'center' | 'left' | 'right';
};
tooltip: {
trigger: 'axis' | 'item' | 'none';
};
legend: {
data: string[];
};
grid: {
left: string;
right: string;
bottom: string;
containLabel: boolean;
};
xAxis: {
type: 'category';
boundaryGap: boolean;
data: string[];
};
yAxis: {
type: 'value';
};
series: LineChartSeriesItem[];
}
3. Scatter Chart
- Supports Multiple Series: YES. Scatter charts can be used to visualize the relationship between two variables for different groups or categories of data.
{
"title": {
"text": "Scatter Chart Title",
"left": "center"
},
"tooltip": {
"trigger": "item",
"formatter": "{a} <br/>{b} : ({c})"
},
"legend": {
"data": ["Series 1", "Series 2"]
},
"xAxis": {
"type": "value"
},
"yAxis": {
"type": "value"
},
"series": [
{
"name": "Series 1",
"type": "scatter",
"data": [
["seriesDataPlaceholder1X", "seriesDataPlaceholder1Y"],
["seriesDataPlaceholder2X", "seriesDataPlaceholder2Y"],
["seriesDataPlaceholder3X", "seriesDataPlaceholder3Y"]
]
},
{
"name": "Series 2",
"type": "scatter",
"data": [
["seriesDataPlaceholder4X", "seriesDataPlaceholder4Y"],
["seriesDataPlaceholder5X", "seriesDataPlaceholder5Y"],
["seriesDataPlaceholder6X", "seriesDataPlaceholder6Y"]
]
}
]
}
interface ScatterChartSeriesItem {
name: string;
type: 'scatter';
data: (string | number)[][]; // Array of [x, y] pairs
}
interface ScatterChartOption {
title: {
text: string;
left: 'center' | 'left' | 'right';
};
tooltip: {
trigger: 'axis' | 'item' | 'none';
formatter: string;
};
legend: {
data: string[];
};
xAxis: {
type: 'value';
};
yAxis: {
type: 'value';
};
series: ScatterChartSeriesItem[];
}
4. Pie Chart
- Supports Multiple Series (Less Common): YES, but less typically used than other charts. While ECharts does allow multiple pie chart series (often arranged as nested pies), it's usually clearer to represent different categories within one pie chart series. If you need to compare different groups' overall proportions, then multiple pie chart series might make sense. However, for simple comparisons, stacked bar charts or 100% stacked bar charts might be better.
{
"title": {
"text": "Pie Chart Title",
"left": "center"
},
"tooltip": {
"trigger": "item",
"formatter": "{a} <br/>{b} : {c} ({d}%)"
},
"legend": {
"orient": "vertical",
"left": "left",
"data": ["Category A", "Category B", "Category C"]
},
"series": [
{
"name": "Series 1",
"type": "pie",
"radius": "55%",
"center": ["50%", "60%"],
"data": [
{ "value": "seriesDataPlaceholder1", "name": "Category A" },
{ "value": "seriesDataPlaceholder2", "name": "Category B" },
{ "value": "seriesDataPlaceholder3", "name": "Category C" }
],
"emphasis": {
"itemStyle": {
"shadowBlur": 10,
"shadowOffsetX": 0,
"shadowColor": "rgba(0, 0, 0, 0.5)"
}
}
}
]
}
interface PieChartSeriesItemData {
value: string | number;
name: string;
}
interface PieChartSeriesItem {
name: string;
type: 'pie';
radius: string;
center: string[]; // Array of two strings (e.g., ["50%", "50%"])
data: PieChartSeriesItemData[];
emphasis: {
itemStyle: {
shadowBlur: number;
shadowOffsetX: number;
shadowColor: string;
};
};
}
interface PieChartOption {
title: {
text: string;
left: 'center' | 'left' | 'right';
};
tooltip: {
trigger: 'axis' | 'item' | 'none';
formatter: string;
};
legend: {
orient: 'vertical' | 'horizontal';
left: 'left' | 'center' | 'right';
data: string[];
};
series: PieChartSeriesItem[];
}
How to Use This Structure
-
Create Your Data: Prepare your data in a format that can be easily mapped into the "seriesDataPlaceholder" locations. This data might come from an API, a database, or a local file.
-
JSON.stringify() (if needed): Convert your data to JSON format. Many front-end frameworks have built-in methods for this. If you're directly using JavaScript, you'll use JSON.stringify().
-
Replace Placeholders: Iterate through your data and replace the "seriesDataPlaceholder" values with your actual data values. You can do this programmatically using JavaScript or the templating language of your front-end framework. For example (in JavaScript):
const myData = [10, 20, 30];
let barChartOption = JSON.parse(JSON.stringify(barChartTemplate)); // Deep copy
barChartOption.series[0].data = myData; // Replace with your data.
// or for multiple series:
const series1Data = [1,2,3];
const series2Data = [4,5,6];
let multiSeriesBarChartOption = JSON.parse(JSON.stringify(barChartTemplate));
multiSeriesBarChartOption.series[0].data = series1Data;
multiSeriesBarChartOption.series[1].data = series2Data;
echarts.init(document.getElementById('main')).setOption(barChartOption); // Example using the generated JSON
-
ECharts Integration: Pass the modified JSON option object to the echarts.setOption() method to render your chart. Make sure you've already initialized your ECharts instance.
Important Notes
- Customization: These templates provide a basic structure. You can (and should!) customize them further by adding more ECharts options, such as colors, labels, tooltips, and visual effects. Refer to the ECharts documentation for the full range of options.
- Data Types: Be mindful of the data types you're inserting into the
data arrays. ECharts expects numbers or strings, so ensure your data is in the correct format before injecting it.
- Deep Copy: When working with JavaScript objects, especially nested ones, it's often best to create a deep copy of your template JSON before modifying it. This prevents accidental modification of the original template. The
JSON.parse(JSON.stringify(template)) method is a common way to create a deep copy, but you could also use a library like Lodash's _.cloneDeep() if you're already using Lodash.
This comprehensive approach should give you a solid foundation for working with ECharts and creating consistent, data-driven visualizations. Remember to consult the ECharts documentation for more advanced customization options.