Changing data symbol when using ECharts dataset format - echarts

ECharts allows defining different symbol for each data point when using series.data option like in this example:
option = {
xAxis: {
type: 'time'
},
yAxis: {
type: 'value'
},
series: [
{
symbolSize: 50,
data: [
{ value: ['2023-01-01T15:00:00Z', 10], symbol: 'circle' },
{ value: ['2023-01-01T16:00:00Z', 20], symbol: 'rect' },
{ value: ['2023-01-01T17:00:00Z', 15], symbol: 'triangle' }
],
type: 'line'
}
]
};
This could be used e.g. to display weather symbol in a weather forecast chart. The example defines data under series, however it seems to be an older approach and now it is recommended to use datasets instead.
How to set the symbol per datapoint when providing data in dataset.source?

series.symbol can be a callback function which can access the data from dataset and return different symbol for each data point:
If symbols needs to be different, you can set with callback function
in the following format:
(value: Array|number, params: Object) => string The first parameter
value is the value in data, and the second parameter params is the
rest parameters of data item.
So using dataset and callback, the code becomes:
option = {
dataset: {
source: [
// Each entry has 3rd value set to the symbol name.
['2023-01-01T15:00:00Z', 10, 'circle'],
['2023-01-01T16:00:00Z', 20, 'rect'],
['2023-01-01T17:00:00Z', 15, 'triangle'],
]
},
xAxis: {
type: 'time'
},
yAxis: {
type: 'value'
},
series: [
{
symbolSize: 50,
// The first parameter 'value' is the entry in dataset.
// 3rd value (value[2]) is the symbol name.
symbol: (value, params) => value[2],
type: 'line'
}
]
};

Related

ECharts, Gauge, multiple pointers and dataset

I'm trying to recreate the ECharts "Multi Title Gauge" example (a gauge with three pointers in different colors) but using a dataset rather than the gauge data values in the series.data definition. While I can get this to work for a gauge with a single pointer, I cannot get it working for the "Multi Title Gauge".
I've been able to get this to work with line, bar and pie charts.
I did read that the datasets feature, while introduced in 5.0, is not supported by all chart types.
I was encouraged when I was able to get "Gauge Basic Chart" working with a dataset. But have not been able to get "Multi Title Gauge" to work with a dataset.
The closest I have gotten to having "Multi Title Gauge" use a dataset is by using multiple series' and datasets. Each dataset has only 1 value.
ChartOptions={
dataset: [
{ source: [ ["Series1"], [46] ] },
{ source: [ ["Series2"], [56] ] }
],
series: [
{
name: "Series1",
type: "gauge",
datasetIndex: 0,
progress: { show: true },
title: { offsetCenter: ["-60%", "82%"] },
detail: {
formatter: "R1",
offsetCenter: ["-60%", "97%"]
},
min: 0,
max: 180,
splitNumber: 9,
axisTick: { splitNumber: 4 }
},
{
name: "Series2",
type: "gauge",
datasetIndex: 1,
progress: { show: true },
title: { offsetCenter: ["0%", "82%"] },
detail: {
formatter: "R2",
offsetCenter: ["0%", "97%"]
},
min: 0,
max: 180,
splitNumber: 9,
axisTick: { splitNumber: 4 }
}
]
}
However, both pointers are blue - there is no automatic color change for the different series' and issues with the legend. There is quite a bit of repetition in the data configuration. Using a dataset would (as designed) separate the config from the data.
I have also tried a single dataset with multiple values and then using 'encode' in the series, but 'encode' seems to be ignored for Gauge.
My preference is to standardize on using a dataset for all my charts. But if it's not fully supported for Gauge, then I'll take a different approach.
Any insight would be appreciated.

Can I disable first of month/year labels for a xAxis of type 'time' to get even intervals?

When I have a chart over several months, the interval is set to include the first of the month making intervals uneven and causing labels to overlap. Is this the expected behavior? Is there a way to disable this?
example
Here is an example based off one of echarts examples.
option = {
xAxis: {
type: 'time',
},
yAxis: {
type: 'value',
},
series: [
{
type: 'line',
symbol: 'none',
lineStyle: {
color: '#5470C6',
width: 5
},
data: [
['2019-08-17', 200],
['2019-10-10', 200],
['2019-10-11', 560],
['2019-10-12', 750],
['2019-10-13', 580],
['2019-10-14', 250],
['2019-10-15', 300],
['2019-10-16', 450],
['2019-10-17', 300],
['2019-11-15', 100]
]
}
]
};
If you want even intervals, you can instruct echarts to treat the dates as discrete values by changing the configuration of the axis:
xAxis: {
type: 'category',
}

Question about colorBy after data sorting

In this example with universalTransition turned on, after the pie chart of colorBy:'data' is sorted, it is inconsistent with the corresponding relationship between labels and colors in the bar chart, how to make their colors consistent.
Makepie will be out of service on February 15, you can run follow code on ECharts examples editor.
const dataset = {
dimensions: ['name', 'score'],
source: [
['Hannah Krause', 314],
['Zhao Qian', 351],
]
};
const pieOption = {
// dataset: [dataset],
// 顺序排序数据
dataset: [dataset].concat({
transform: {
type: 'sort',
config: { dimension: 'score', order: 'desc' },
},
}),
series: [
{
type: 'pie',
// 通过 id 关联需要过渡动画的系列
id: 'Score',
radius: [0, '50%'],
universalTransition: true,
animationDurationUpdate: 1000,
// 取排序后的数据
datasetIndex: 1,
}
]
};
const barOption = {
dataset: [dataset],
xAxis: {
type: 'category'
},
yAxis: {},
series: [
{
type: 'bar',
// 通过 id 关联需要过渡动画的系列
id: 'Score',
// 每个数据都是用不同的颜色
colorBy: 'data',
encode: { x: 'name', y: 'score' },
universalTransition: true,
animationDurationUpdate: 1000
}
]
};
option = barOption;
setInterval(() => {
option = option === pieOption ? barOption : pieOption;
// 使用 notMerge 的形式可以移除坐标轴
myChart.setOption(option, true);
}, 2000);
Your dataset order by 'desc' on pie chart.
but it's not used on bar chart.
Maybe your two charts should be sorted in the same order
dataset: [dataset].concat({
transform: {
type: 'sort',
config: { dimension: 'score', order: 'desc' },
},
}),

Multiple bars with same category name show differently based on array of arrays or array of objects

I noticed that using
option = {
xAxis: {
type: 'category',
},
yAxis: {
type: 'value'
},
series: [ {
type: 'bar',
data: [
{ name: 'Sun', value: 50 },
{ name: 'Sun', value: 80 },
{ name: 'Mon', value: 100 }
]
}]
}
There are two bars with category as 'Sun'. But if I use
option = {
xAxis: {
type: 'category',
},
yAxis: {
type: 'value'
},
series: [{
type: 'bar',
data: [
['Sun',50],
['Sun',80],
['Mon',100]
]
}]
}
I see only one bar for 'Sun' with both data elements overlapping. Why does it work with array of objects differently? Also, if instead of using inline data, if I use dataset, then both array of arrays and array of objects behave the same where there is only one bar with overlap.
The reason I am looking for clarity on this is, I have a visualMap with a different attribute for color which makes the data repeat. I am trying to figure out if I need to create separate series or use the same series.
In general is bad idea to naming data points the same name. Strange that the data in the first case did not merge... If you need make the group then in the Echarts it's named stack. For the stack you can use the same name and it will be joined into one group.
VisualMap usually the global option and affects all series but you can limit with seriesIndex.
var myChart = echarts.init(document.getElementById('main'));
var option = {
xAxis: {
data: ["Cat1", "Cat2", "Cat3", "Cat4", "Cat5", "Cat6"]
},
visualMap: {
type: 'continuous',
range: [0, 70],
seriesIndex: 2,
color: ['green', 'blue', 'red'],
},
yAxis: {},
series: [{
name: 'Series1',
stack: 'group1',
type: 'bar',
data: [5, 20, 36, 10, 10, 20],
color: 'lightgray'
}, {
name: 'Series2',
stack: 'group1',
type: 'bar',
data: [5, 20, 36, 10, 10, 20],
color: 'gray'
}, {
name: 'Series3',
stack: 'group2',
type: 'bar',
data: [50, 20, 70, 10, 60, 40],
}]
};
myChart.setOption(option);
<script src="https://cdn.jsdelivr.net/npm/echarts#4.9.0/dist/echarts.min.js"></script>
<div id="main" style="width: 600px;height:400px;"></div>

Is there a wey to use a dictionary object as data in echarts?

I'm using echarts to plot my data. Then I have two data which has different time(which is X axis),and I want to plot them in one picture. But I found that echart's data just can be a array,So this two data's value will not plot right as their real time.
So my questions is that can echarts use a dictionary data?which use time as key.Then the two data will plot at right time.
try using the type 'time' on xAxis. See also the new way to pass data to the chart using dataset:
var myChart = echarts.init(document.getElementById('main'));
// format with arrays:
var source = [
['2019-08-28T07:01:00+02:00', 127.7],
['2019-08-28T07:02:00+02:00', 148.0],
['2019-08-28T07:03:00+02:00', 1180.4],
['2019-08-28T07:04:00+02:00', 117.9]
]
// format with objects:
// var source = [
// { datetime: '2019-08-28T07:01:00+02:00', value: 127.7 },
// { datetime: '2019-08-28T07:02:00+02:00', value: 148.0 },
// { datetime: '2019-08-28T07:03:00+02:00', value: 1180.4 },
// { datetime: '2019-08-28T07:04:00+02:00', value: 117.9 }
// ];
option = {
dataset: {
dimensions: ['datetime', 'value'],
source: source
},
xAxis: {
type: 'time'
},
yAxis: {
type: 'value'
},
series: [{
name: 'series1',
type: 'line',
}]
}
myChart.setOption(option);