const rawData = [ ['Income', 'Name', 'Year', 'Country'] [127931.772548, '生产一班', 2011, 'abc'], [127931.772548, '生产一班', 2012, 'abc'], [127931.772548, '生产一班', 2013, 'abc'], [127931.772548, '生产一班', 2014, 'abc'], [127931.772548, '生产一班', 2015, 'abc'], [127931.772548, '生产一班', 2016, 'abc'], [127931.772548, '生产一班', 2017, 'abc'], [127931.772548, '生产一班', 2018, 'abc'] ] const countries = ['abc'] const datasetWithFilters = [] const seriesList = [] echarts.util.each(countries, function(country) { var datasetId = 'dataset' + country datasetWithFilters.push({ id: datasetId, fromDatasetId: 'dataset_raw', transform: { type: 'filter', config: { and: [ { dimension: 'Country', '=': country } ] } } }) seriesList.push({ type: 'line', datasetId: datasetId, showSymbol: false, endLabel: { show: true, formatter: function(params) { return params.value[1] + ': ' + params.value[0] } }, encode: { x: 'Year', y: 'Income', label: ['Country', 'Income'], itemName: 'Year', tooltip: ['Income'] } }) }) var divdom = this.$refs.divchart if (!divdom) { return } console.log(datasetWithFilters) this.chart = echarts.init(this.$refs.divchart, 'dark') this.chart.setOption({ backgroundColor: 'rgba(255, 255, 255, 0)', animationDuration: 10000, dataset: [ { id: 'dataset_raw', source: _rawData }, ...datasetWithFilters ], tooltip: { order: 'valueDesc', trigger: 'axis' }, xAxis: { type: 'category', nameLocation: 'middle' }, yAxis: { name: 'Income' }, grid: { right: 140 }, series: seriesList })以上代码哪里有问题
时间: 2023-08-06 07:27:25 浏览: 170
代码中存在一些问题,包括:
1. 第一行的数组声明不完整,应该在外层加上方括号,即:
```
const rawData = [
['Income', 'Name', 'Year', 'Country'],
[127931.772548, '生产一班', 2011, 'abc'],
[127931.772548, '生产一班', 2012, 'abc'],
[127931.772548, '生产一班', 2013, 'abc'],
[127931.772548, '生产一班', 2014, 'abc'],
[127931.772548, '生产一班', 2015, 'abc'],
[127931.772548, '生产一班', 2016, 'abc'],
[127931.772548, '生产一班', 2017, 'abc'],
[127931.772548, '生产一班', 2018, 'abc']
];
```
2. 变量 `_rawData` 未定义,应该改为 `rawData`。
3. `echarts.util.each()` 方法应该改为 `echarts.util.eachAxisDim()` 或 `echarts.util.eachAxis()` 方法。
4. 在 `seriesList` 中,`itemName` 应该改为 `name`。
修改后的代码如下:
```
const rawData = [
['Income', 'Name', 'Year', 'Country'],
[127931.772548, '生产一班', 2011, 'abc'],
[127931.772548, '生产一班', 2012, 'abc'],
[127931.772548, '生产一班', 2013, 'abc'],
[127931.772548, '生产一班', 2014, 'abc'],
[127931.772548, '生产一班', 2015, 'abc'],
[127931.772548, '生产一班', 2016, 'abc'],
[127931.772548, '生产一班', 2017, 'abc'],
[127931.772548, '生产一班', 2018, 'abc']
];
const countries = ['abc'];
const datasetWithFilters = [];
const seriesList = [];
echarts.util.eachAxisDim(countries, function(country) {
var datasetId = 'dataset' + country;
datasetWithFilters.push({
id: datasetId,
fromDatasetId: 'dataset_raw',
transform: {
type: 'filter',
config: {
and: [
{
dimension: 'Country',
'=': country
}
]
}
}
});
seriesList.push({
type: 'line',
datasetId: datasetId,
showSymbol: false,
endLabel: {
show: true,
formatter: function(params) {
return params.value[1] + ': ' + params.value[0];
}
},
encode: {
x: 'Year',
y: 'Income',
label: ['Country', 'Income'],
name: 'Year',
tooltip: ['Income']
}
});
});
var divdom = this.$refs.divchart;
if (!divdom) {
return;
}
console.log(datasetWithFilters);
this.chart = echarts.init(this.$refs.divchart, 'dark');
this.chart.setOption({
backgroundColor: 'rgba(255, 255, 255, 0)',
animationDuration: 10000,
dataset: [
{
id: 'dataset_raw',
source: rawData
},
...datasetWithFilters
],
tooltip: {
order: 'valueDesc',
trigger: 'axis'
},
xAxis: {
type: 'category',
nameLocation: 'middle'
},
yAxis: {
name: 'Income'
},
grid: {
right: 140
},
series: seriesList
});
```
阅读全文