prepare-echarts-data
v1.0.0
Published
A utility to prepare data for ECharts visualizations, supporting various aggregation methods.
Maintainers
Readme
prepare-echarts-data
English | 中文说明见下方
prepare-echarts-data is a JavaScript utility for processing input data to generate a format suitable for ECharts. It allows for aggregation of values based on specified dimensions and value fields, supporting various aggregation methods such as sum, average, count, min, and max.
Installation
You can install the package via npm:
npm install prepare-echarts-dataUsage
To use the prepareEChartsDataWithFullAxis function, you need to import it into your project. Here’s an example of how to do that:
import prepareEChartsDataWithFullAxis from 'prepare-echarts-data';
// Sample data
const data = [
{ name: '张三', type: 2, haighe: 170, weight: 30 },
{ name: '张三', type: 1, haighe: 150, weight: 40 },
{ name: '李四', type: 2, haighe: 180, weight: 20 },
{ name: '李四', type: 1, haighe: 200, weight: 60 },
{ name: '张三', type: 2, haighe: 170, weight: 35 }
];
const dimensions = ['name'];
const valueFields = ['weight', 'haighe'];
// Define full X-axis information
const fullXAxis = ['张三', '李四', '王五', '郑六'];
// Generate ECharts data format
const chartData = prepareEChartsDataWithFullAxis(data, dimensions, valueFields, fullXAxis, 'avg', 0);
console.log('chartData:', chartData);
/* {
chartData: {
xAxisData: ['张三', '李四', '王五', '郑六'],
seriesData: {
weight: [35, 40, 0, 0],
haighe: [163.33333333333334, 190, 0, 0]
},
indexMap: Map(4) { '张三' => 0, '李四' => 1, '王五' => 2, '郑六' => 3 }
}
} */API
prepareEChartsDataWithFullAxis(data, dimensions, valueFields, fullXAxis, aggregation, defaultValue)
- data: An array of objects containing the data to be processed.
- dimensions: An array of strings representing the dimensions to group by.
- valueFields: An array of strings representing the fields to aggregate.
- fullXAxis: An array of strings representing the complete X-axis labels.
- aggregation: A string indicating the aggregation method ('sum', 'avg', 'count', 'min', 'max').
- defaultValue: The default value to use for missing data points.
中文说明
prepare-echarts-data 是一个用于处理输入数据并生成适用于 ECharts 格式的 JavaScript 工具。它支持根据指定的维度和数值字段进行聚合,支持多种聚合方式,如求和、平均值、计数、最小值和最大值。
安装
通过 npm 安装:
npm install prepare-echarts-data用法示例
在项目中引入并使用 prepareEChartsDataWithFullAxis 方法:
import prepareEChartsDataWithFullAxis from 'prepare-echarts-data';
// 示例数据
const data = [
{ name: '张三', type: 2, haighe: 170, weight: 30 },
{ name: '张三', type: 1, haighe: 150, weight: 40 },
{ name: '李四', type: 2, haighe: 180, weight: 20 },
{ name: '李四', type: 1, haighe: 200, weight: 60 },
{ name: '张三', type: 2, haighe: 170, weight: 35 }
];
const dimensions = ['name'];
const valueFields = ['weight', 'haighe'];
// 定义完整的 X 轴标签
const fullXAxis = ['张三', '李四', '王五', '郑六'];
// 生成 ECharts 所需数据格式
const chartData = prepareEChartsDataWithFullAxis(data, dimensions, valueFields, fullXAxis, 'avg', 0);
console.log('chartData:', chartData);
/* {
chartData: {
xAxisData: ['张三', '李四', '王五', '郑六'],
seriesData: {
weight: [35, 40, 0, 0],
haighe: [163.33333333333334, 190, 0, 0]
},
indexMap: Map(4) { '张三' => 0, '李四' => 1, '王五' => 2, '郑六' => 3 }
}
} */API
prepareEChartsDataWithFullAxis(data, dimensions, valueFields, fullXAxis, aggregation, defaultValue)
- data:待处理的数据对象数组。
- dimensions:用于分组的维度字段数组。
- valueFields:需要聚合的数值字段数组。
- fullXAxis:完整的 X 轴标签数组。
- aggregation:聚合方式,可选值有 'sum'(求和)、'avg'(平均)、'count'(计数)、'min'(最小值)、'max'(最大值)。
- defaultValue:缺失数据时的默认值。
License
This project is licensed under the MIT License. See the LICENSE file for details.
