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
Chart.js API - Chart.js中文文档 - 笔下光年
网站首页
Chart.js API
For each chart, there are a set of global prototype methods on the shared chart type which you may find useful. These are available on all charts created with Chart.js, but for the examples, let's use a line chart we've made. ```javascript // For example: const myLineChart = new Chart(ctx, config); ``` ## .destroy() Use this to destroy any chart instances that are created. This will clean up any references stored to the chart object within Chart.js, along with any associated event listeners attached by Chart.js. This must be called before the canvas is reused for a new chart. ```javascript // Destroys a specific chart instance myLineChart.destroy(); ``` ## .update(mode?) Triggers an update of the chart. This can be safely called after updating the data object. This will update all scales, legends, and then re-render the chart. ```javascript myLineChart.data.datasets[0].data[2] = 50; // Would update the first dataset's value of 'March' to be 50 myLineChart.update(); // Calling update now animates the position of March from 90 to 50. ``` A mode string can be provided to indicate transition configuration should be used. Core calls this method using any of 'active', 'hide', 'reset', 'resize', 'show' or undefined. 'none' is also a supported mode for skipping animations for single update. Please see animations docs for more details. Example: ```javascript myChart.update('active'); ``` See Updating Charts for more details. ## .reset() Reset the chart to its state before the initial animation. A new animation can then be triggered using update. ```javascript myLineChart.reset(); ``` ## .render() Triggers a redraw of all chart elements. Note, this does not update elements for new data. Use .update() in that case. ## .stop() Use this to stop any current animation. This will pause the chart during any current animation frame. Call .render() to re-animate. ```javascript // Stops the charts animation loop at its current frame myLineChart.stop(); // => returns 'this' for chainability ``` ## .resize(width?, height?) Use this to manually resize the canvas element. This is run each time the canvas container is resized, but you can call this method manually if you change the size of the canvas nodes container element. You can call .resize() with no parameters to have the chart take the size of its container element, or you can pass explicit dimensions (e.g., for printing). ```javascript // Resizes & redraws to fill its container element myLineChart.resize(); // => returns 'this' for chainability // With an explicit size: myLineChart.resize(width, height); ``` ## .clear() Will clear the chart canvas. Used extensively internally between animation frames, but you might find it useful. ```javascript // Will clear the canvas that myLineChart is drawn on myLineChart.clear(); // => returns 'this' for chainability ``` ## .toBase64Image(type?, quality?) This returns a base 64 encoded string of the chart in its current state. ```javascript myLineChart.toBase64Image(); // => returns png data url of the image on the canvas myLineChart.toBase64Image('image/jpeg', 1) // => returns a jpeg data url in the highest quality of the canvas ``` ## .getElementsAtEventForMode(e, mode, options, useFinalPosition) Calling getElementsAtEventForMode(e, mode, options, useFinalPosition) on your Chart instance passing an event and a mode will return the elements that are found. The options and useFinalPosition arguments are passed through to the handlers. To get an item that was clicked on, getElementsAtEventForMode can be used. ```javascript function clickHandler(evt) { const points = myChart.getElementsAtEventForMode(evt, 'nearest', { intersect: true }, true); if (points.length) { const firstPoint = points[0]; const label = myChart.data.labels[firstPoint.index]; const value = myChart.data.datasets[firstPoint.datasetIndex].data[firstPoint.index]; } } ``` ## .getSortedVisibleDatasetMetas() Returns an array of all the dataset meta's in the order that they are drawn on the canvas that are not hidden. ```javascript const visibleMetas = chart.getSortedVisibleDatasetMetas(); ``` ## .getDatasetMeta(index) Looks for the dataset that matches the current index and returns that metadata. This returned data has all of the metadata that is used to construct the chart. The data property of the metadata will contain information about each point, bar, etc. depending on the chart type. Extensive examples of usage are available in the Chart.js tests . ```javascript const meta = myChart.getDatasetMeta(0); const x = meta.data[0].x; ``` ## getVisibleDatasetCount Returns the amount of datasets that are currently not hidden. ```javascript const numberOfVisibleDatasets = chart.getVisibleDatasetCount(); ``` ## setDatasetVisibility(datasetIndex, visibility) Sets the visibility for a given dataset. This can be used to build a chart legend in HTML. During click on one of the HTML items, you can call setDatasetVisibility to change the appropriate dataset. ```javascript chart.setDatasetVisibility(1, false); // hides dataset at index 1 chart.update(); // chart now renders with dataset hidden ``` ## toggleDataVisibility(index) Toggles the visibility of an item in all datasets. A dataset needs to explicitly support this feature for it to have an effect. From internal chart types, doughnut / pie, polar area, and bar use this. ```javascript chart.toggleDataVisibility(2); // toggles the item in all datasets, at index 2 chart.update(); // chart now renders with item hidden ``` ## getDataVisibility(index) Returns the stored visibility state of an data index for all datasets. Set by toggleDataVisibility. A dataset controller should use this method to determine if an item should not be visible. ```javascript const visible = chart.getDataVisibility(2); ``` ## hide(datasetIndex, dataIndex?) If dataIndex is not specified, sets the visibility for the given dataset to false. Updates the chart and animates the dataset with 'hide' mode. This animation can be configured under the hide key in animation options. Please see animations docs for more details. If dataIndex is specified, sets the hidden flag of that element to true and updates the chart. ```javascript chart.hide(1); // hides dataset at index 1 and does 'hide' animation. chart.hide(0, 2); // hides the data element at index 2 of the first dataset. ``` ## show(datasetIndex, dataIndex?) If dataIndex is not specified, sets the visibility for the given dataset to true. Updates the chart and animates the dataset with 'show' mode. This animation can be configured under the show key in animation options. Please see animations docs for more details. If dataIndex is specified, sets the hidden flag of that element to false and updates the chart. ```javascript chart.show(1); // shows dataset at index 1 and does 'show' animation. chart.show(0, 2); // shows the data element at index 2 of the first dataset. ``` ## setActiveElements(activeElements) Sets the active (hovered) elements for the chart. See the "Programmatic Events" sample file to see this in action. ```javascript chart.setActiveElements([ {datasetIndex: 0, index: 1}, ]); ``` ## isPluginEnabled(pluginId) Returns a boolean if a plugin with the given ID has been registered to the chart instance. ```javascript chart.isPluginEnabled('filler'); ``` ## Static: getChart(key) Finds the chart instance from the given key. If the key is a string, it is interpreted as the ID of the Canvas node for the Chart. The key can also be a CanvasRenderingContext2D or an HTMLDOMElement. This will return undefined if no Chart is found. To be found, the chart must have previously been created. ```javascript const chart = Chart.getChart("canvas-id"); ``` ## Static: register(chartComponentLike) Used to register plugins, axis types or chart types globally to all your charts. ```javascript import { Chart, Tooltip, LinearScale, PointElement, BubbleController } from 'chart.js'; Chart.register(Tooltip, LinearScale, PointElement, BubbleController); ``` ## Static: unregister(chartComponentLike) Used to unregister plugins, axis types or chart types globally from all your charts.
上一篇:
开发者(Developers)
下一篇:
坐标轴扩展