Chart.js
入门指南
入门
安装
集成
分步指南
概览
可访问性(Accessibility)
颜色(Colors)
数据结构(Data structures)
字体(Fonts)
选项(Options)
内边距(Padding)
性能(Performance)
图表配置
配置(Configuration)
动画(Animations)
画布背景(Canvas background)
数据抽取(Data Decimation)
设备像素比率(Device Pixel Ratio)
通用配置(Elements)
互动(Interactions)
布局(Layout)
图例(Legend)
本地化(Locale)
响应式图表(Responsive Charts)
副标题(Subtitle)
标题(Title)
提示(Tooltip)
Charts
面积图(Area Chart)
柱状/条形图(Bar Chart)
气泡图(Bubble Chart)
环形&饼图(Doughnut and Pie Charts)
折线图(Line Chart)
混合图表(Mixed Chart Types)
极地图(Polar Area Chart)
雷达图(Radar Chart)
离散图(Scatter Chart)
坐标轴
轴(Axes)
笛卡尔坐标(Cartesian)
笛卡尔坐标轴(Cartesian Axes)
分类轴(Category Axis)
线性轴(Linear Axis)
对数轴(Logarithmic Axis)
时间笛卡尔轴(Time Cartesian Axis)
时间序列轴(Time Series Axis)
径向(Radial)
径向轴(Radial Axes)
线性径向轴(Linear Radial Axis)
标签轴(Labelling Axes)
样式(Styling)
开发者
开发者(Developers)
Chart.js API
坐标轴扩展
图表扩展
贡献
插件
发布扩展
更新 Charts
迁移
4.x迁移指南
3.x迁移指南
示例
Chart.js Samples
Bar Charts
Bar Chart Border Radius
Floating Bars
Horizontal Bar Chart
Stacked Bar Chart
Stacked Bar Chart with Groups
Vertical Bar Chart
Line Charts
Interpolation Modes
Line Chart
Multi Axis Line Chart
Point Styling
Line Segment Styling
Stepped Line Charts
Line Styling
Other charts
Bubble
Combo bar/line
Doughnut
Multi Series Pie
Pie
Polar area
Polar area centered point labels
Radar
Radar skip points
Scatter
Scatter - Multi axis
Stacked bar/line
Area charts
Line Chart Boundaries
Line Chart Datasets
Line Chart drawTime
Line Chart Stacked
Radar Chart Stacked
Scales
Linear Scale - Min-Max
Linear Scale - Suggested Min-Max
Linear Scale - Step Size
Log Scale
Stacked Linear / Category
Time Scale
Time Scale - Max Span
Time Scale - Combo Chart
Scale Options
Center Positioning
Grid Configuration
Tick Configuration
Title Configuration
Legend
Events
HTML Legend
Point Style
Position
Alignment and Title Position
Title
Alignment
Subtitle
Basic
Tooltip
Custom Tooltip Content
External HTML Tooltip
Interaction Modes
Point Style
Position
Scriptable Options
Bar Chart
Bubble Chart
Line Chart
Pie Chart
Polar Area Chart
Radar Chart
Animations
Delay
Drop
Loop
Progressive Line
Progressive Line With Easing
Advanced
Data Decimation
Derived Axis Type
Derived Chart Type
Linear Gradient
Programmatic Event Triggers
Animation Progress Bar
Radial Gradient
Plugins
Chart Area Border
Doughnut Empty State
Quadrants
Utils
数据结构(Data structures) - Chart.js中文文档 - 笔下光年
网站首页
数据结构(Data structures)
The data property of a dataset can be passed in various formats. By default, that data is parsed using the associated chart type and scales. If the labels property of the main data property is used, it has to contain the same amount of elements as the dataset with the most values. These labels are used to label the index axis (default x axes). The values for the labels have to be provided in an array. The provided labels can be of the type string or number to be rendered correctly. In case you want multiline labels you can provide an array with each line as one entry in the array. ## Primitive[] ```javascript const cfg = { type: 'bar', data: { datasets: [{ data: [20, 10], }], labels: ['a', 'b'] } } ``` When the data is an array of numbers, values from labels array at the same index are used for the index axis (x for vertical, y for horizontal charts). ## Object[] ```javascript const cfg = { type: 'line', data: { datasets: [{ data: [{x: 10, y: 20}, {x: 15, y: null}, {x: 20, y: 10}] }] } } ``` ```javascript const cfg = { type: 'line', data: { datasets: [{ data: [{x: '2016-12-25', y: 20}, {x: '2016-12-26', y: 10}] }] } } ``` ```javascript const cfg = { type: 'bar', data: { datasets: [{ data: [{x: 'Sales', y: 20}, {x: 'Revenue', y: 10}] }] } } ``` This is also the internal format used for parsed data. In this mode, parsing can be disabled by specifying parsing: false at chart options or dataset. If parsing is disabled, data must be sorted and in the formats the associated chart type and scales use internally. The values provided must be parsable by the associated scales or in the internal format of the associated scales. A common mistake would be to provide integers for the category scale, which uses integers as an internal format, where each integer represents an index in the labels array. null can be used for skipped values. ## Object[] using custom properties ```javascript const cfg = { type: 'bar', data: { datasets: [{ data: [{id: 'Sales', nested: {value: 1500}}, {id: 'Purchases', nested: {value: 500}}] }] }, options: { parsing: { xAxisKey: 'id', yAxisKey: 'nested.value' } } } ``` When using the pie/doughnut, radar or polarArea chart type, the parsing object should have a key item that points to the value to look at. In this example, the doughnut chart will show two items with values 1500 and 500. ```javascript const cfg = { type: 'doughnut', data: { datasets: [{ data: [{id: 'Sales', nested: {value: 1500}}, {id: 'Purchases', nested: {value: 500}}] }] }, options: { parsing: { key: 'nested.value' } } } ``` If the key contains a dot, it needs to be escaped with a double slash: ```javascript const cfg = { type: 'line', data: { datasets: [{ data: [{'data.key': 'one', 'data.value': 20}, {'data.key': 'two', 'data.value': 30}] }] }, options: { parsing: { xAxisKey: 'data\\.key', yAxisKey: 'data\\.value' } } } ``` > WARNING When using object notation in a radar chart you still need a labels array with labels for the chart to show correctly. ## Object ```javascript const cfg = { type: 'line', data: { datasets: [{ data: { January: 10, February: 20 } }] } } ``` In this mode, property name is used for index scale and value for value scale. For vertical charts, index scale is x and value scale is y. ## Dataset Configuration | Name | Type | Description | |---------|----------------|---------------------------| | label | string | The label for the dataset which appears in the legend and tooltips. | | clip | number | object | How to clip relative to chartArea. Positive value allows overflow, negative value clips that many pixels inside chartArea. 0 = clip at chartArea. Clipping can also be configured per side: clip: {left: 5, top: false, right: -2, bottom: 0} | | order | number | The drawing order of dataset. Also affects order for stacking, tooltip and legend. | | stack | string | The ID of the group to which this dataset belongs to (when stacked, each group will be a separate stack). Defaults to dataset type. | | parsing | boolean | object | How to parse the dataset. The parsing can be disabled by specifying parsing: false at chart options or dataset. If parsing is disabled, data must be sorted and in the formats the associated chart type and scales use internally. | | hidden | boolean | Configure the visibility of the dataset. Using hidden: true will hide the dataset from being rendered in the Chart. | ### parsing ```javascript const data = [{x: 'Jan', net: 100, cogs: 50, gm: 50}, {x: 'Feb', net: 120, cogs: 55, gm: 75}]; const cfg = { type: 'bar', data: { labels: ['Jan', 'Feb'], datasets: [{ label: 'Net sales', data: data, parsing: { yAxisKey: 'net' } }, { label: 'Cost of goods sold', data: data, parsing: { yAxisKey: 'cogs' } }, { label: 'Gross margin', data: data, parsing: { yAxisKey: 'gm' } }] }, }; ``` ## Typescript When using typescript, if you want to use a data structure that is not the default data structure, you will need to pass it to the type interface when instantiating the data variable. ```javascript import {ChartData} from 'chart.js'; const datasets: ChartData <'bar', {key: string, value: number} []> = { datasets: [{ data: [{key: 'Sales', value: 20}, {key: 'Revenue', value: 10}], parsing: { xAxisKey: 'key', yAxisKey: 'value' } }], }; ```
上一篇:
颜色(Colors)
下一篇:
字体(Fonts)