omi/packages/omi-chart/README.md

162 lines
4.8 KiB
Markdown
Raw Normal View History

2018-12-13 10:21:11 +08:00
<p align="right">v<strong>1.0.1</strong></p>
2018-12-12 08:28:19 +08:00
<p align="center"><img src="./assets/omi-chart.svg" alt="omi-chart" width="300"/></p>
2018-12-11 15:01:04 +08:00
<h2 align="center">omi-chart</h2>
2018-12-11 17:37:16 +08:00
<p align="center">Simple HTML5 Charts using <strong>chart-x</strong> tag powered by <a href="https://github.com/Tencent/omi">omi</a> and <a href="https://www.chartjs.org/" rel="nofollow">chart.js</a>.</p>
2018-12-11 14:47:48 +08:00
2018-12-11 15:01:04 +08:00
---
2018-12-11 13:20:36 +08:00
2018-12-12 08:32:23 +08:00
[![omi-chart](./assets/omi-chart2.png)](https://tencent.github.io/omi/packages/omi-chart/repl/index.html)
2018-12-12 08:28:19 +08:00
2018-12-14 13:03:57 +08:00
## DEMO
2018-12-11 13:18:33 +08:00
- Bar charts
- [Simple](https://tencent.github.io/omi/packages/omi-chart/repl/index.html)
2018-12-11 15:11:47 +08:00
- [Vertical](https://tencent.github.io/omi/packages/omi-chart/repl/bar-vertical.html)
2018-12-11 17:25:43 +08:00
- [Horizontal](https://tencent.github.io/omi/packages/omi-chart/repl/bar-horizontal.html)
- [Multi Axis](https://tencent.github.io/omi/packages/omi-chart/repl/bar-multi-axis.html)
2018-12-11 17:32:11 +08:00
- [Stacked Group](https://tencent.github.io/omi/packages/omi-chart/repl/bar-stacked-group.html)
2018-12-11 13:18:33 +08:00
- Line charts
2018-12-11 17:58:40 +08:00
- [Base](https://tencent.github.io/omi/packages/omi-chart/repl/line-base.html)
- [Multi Axis](https://tencent.github.io/omi/packages/omi-chart/repl/line-multi-axis.html)
- [Stepped](https://tencent.github.io/omi/packages/omi-chart/repl/line-stepped.html)
2018-12-12 08:38:15 +08:00
- [Fill](https://tencent.github.io/omi/packages/omi-chart/repl/line-fill.html)
- [Interpolation Modes](https://tencent.github.io/omi/packages/omi-chart/repl/line-interpolation-modes.html)
- [Point Sizes](https://tencent.github.io/omi/packages/omi-chart/repl/line-point-sizes.html)
- [Styles](https://tencent.github.io/omi/packages/omi-chart/repl/line-styles.html)
- [Point Styles](https://tencent.github.io/omi/packages/omi-chart/repl/line-point-styles.html)
2018-12-12 09:50:17 +08:00
- Scatter charts
- [Base](https://tencent.github.io/omi/packages/omi-chart/repl/scatter-base.html)
2018-12-12 10:12:52 +08:00
- [Multi Axis](https://tencent.github.io/omi/packages/omi-chart/repl/radar-multi-axis.html)
- Other charts
- [Pie](https://tencent.github.io/omi/packages/omi-chart/repl/pie.html)
- [Doughnut](https://tencent.github.io/omi/packages/omi-chart/repl/doughnut.html)
- [Radar](https://tencent.github.io/omi/packages/omi-chart/repl/radar.html)
- [PolarArea](https://tencent.github.io/omi/packages/omi-chart/repl/polar-area.html)
2018-12-13 10:21:11 +08:00
- [Bubble](https://tencent.github.io/omi/packages/omi-chart/repl/bubble.html)
2018-12-12 10:12:52 +08:00
- [Bar Line](https://tencent.github.io/omi/packages/omi-chart/repl/bar-line.html)
2018-12-23 07:58:22 +08:00
## Compatible with IE
2018-12-23 08:02:26 +08:00
- [→ Demo using Omio and node.classList polyfill](https://tencent.github.io/omi/packages/omi-chart/repl/ie-test.html)
2018-12-23 07:58:22 +08:00
- [→ Source Code](https://github.com/Tencent/omi/blob/master/packages/omi-chart/repl/ie-test.html)
2018-12-14 09:54:47 +08:00
## Supports type
* `<chart-bar />`
* `<chart-line />`
* `<chart-scatter />`
* `<chart-pie />`
* `<chart-doughnut />`
* `<chart-radar />`
* `<chart-polar-area />`
* `<chart-bubble />`
2018-12-13 10:24:12 +08:00
## Install
```bash
npm i omi-chart
```
2018-12-12 11:18:24 +08:00
## Usage
```js
2019-03-27 14:50:09 +08:00
import { define, WeElement, render } from 'omi'
2018-12-13 10:24:12 +08:00
import 'omi-chart'
2018-12-12 11:18:24 +08:00
define('my-app', class extends WeElement {
install(){
this.chartData = {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: '#f391a9',
borderColor: '#ef5b9c',
borderWidth: 1
}]
}
this.chartOptions = {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
}
render(props, data) {
return (
<div>
<chart-bar width={600} data={this.chartData} options={this.chartOptions} />
</div>
)
}
})
render(<my-app />, 'body')
```
2018-12-14 09:54:47 +08:00
For detailed options and data formats, see the [chart.js document](https://www.chartjs.org/docs/latest/).
2018-12-12 11:18:24 +08:00
2018-12-12 10:12:52 +08:00
## Principle
```js
import { WeElement, define } from 'omi'
import Chart from 'chart.js'
class ChartBase extends WeElement {
receiveProps(props) {
this.chart.data = props.data
this.chart.options = props.options
this.chart.update()
}
render(props) {
return (
<div style={{ width: props.width + 'px', height: props.height + 'px' }}>
<canvas ref={(e) => { this.canvas = e }}>
</canvas>
</div>
)
}
}
define('chart-bar', class extends ChartBase {
installed() {
this.chart = new Chart(this.canvas.getContext('2d'), {
type: this.props.horizontal ? 'horizontalBar' : 'bar',
data: this.props.data,
options: this.props.options
})
}
})
define('chart-line', class extends ChartBase {
installed() {
this.chart = new Chart(this.canvas.getContext('2d'), {
type: 'line',
data: this.props.data,
options: this.props.options
})
}
})
define('chart-scatter', class extends ChartBase {
installed() {
this.chart = new Chart.Scatter(this.canvas.getContext('2d'), {
data: this.props.data,
options: this.props.options
})
}
})
```
## License
MIT