项目作者: aloneszjl

项目描述 :
F2 charts for react-native
高级语言: HTML
项目地址: git://github.com/aloneszjl/react-native-f2chart.git
创建时间: 2019-02-24T09:20:56Z
项目社区:https://github.com/aloneszjl/react-native-f2chart

开源协议:MIT License

下载


react-native-f2chart

F2 charts for react-native

install

1、

  1. yarn add react-native-f2chart or npm i react-native-f2chart

2、 copy node_moules/react-native-f2chart/src/f2chart.html to android/app/src/main/assets/f2chart.html

simple demo example

usage

  1. import Chart from "react-native-f2chart";
  2. // 参考 https://antv.alipay.com/zh-cn/f2/3.x/demo/line/basic.html
  3. const initScript = data =>`
  4. (function(){
  5. chart = new F2.Chart({
  6. id: 'chart',
  7. pixelRatio: window.devicePixelRatio,
  8. });
  9. chart.source(${JSON.stringify(data)}, {
  10. value: {
  11. tickCount: 5,
  12. min: 0
  13. },
  14. date: {
  15. type: 'timeCat',
  16. range: [0, 1],
  17. tickCount: 3
  18. }
  19. });
  20. chart.tooltip({
  21. custom: true,
  22. showXTip: true,
  23. showYTip: true,
  24. snap: true,
  25. onChange: function(obj) {
  26. window.postMessage(stringify(obj))
  27. },
  28. crosshairsType: 'xy',
  29. crosshairsStyle: {
  30. lineDash: [2]
  31. }
  32. });
  33. chart.axis('date', {
  34. label: function label(text, index, total) {
  35. var textCfg = {};
  36. if (index === 0) {
  37. textCfg.textAlign = 'left';
  38. } else if (index === total - 1) {
  39. textCfg.textAlign = 'right';
  40. }
  41. return textCfg;
  42. }
  43. });
  44. chart.line().position('date*value');
  45. chart.render();
  46. })();
  47. `;
  48. ...
  49. render() {
  50. return (
  51. <View style={{ height: 350 }}>
  52. <Chart initScript={initScript(data)} ></Chart>
  53. </View>
  54. )
  55. }
  56. ...
  57. `

Props

Prop type Description Required
initScript string 初始化图表的 js 代码,参考 f2 的文档 yes
data Array f2 chart source no
onChange Function tooltip onchange no
webView ReactElement 渲染图表的 webview,可以使用 react-native-webview 代替,默认使用 react-natve 里面的 webview no

Notice

  • canvas的id 是 ‘chart’,不是文档中的 ‘mountNode’
  • chart 已经在源码的 html 定义过了,在 initScript 中,并不需要定义 chart,直接给 chart 赋值即可
  • tooltip onchange 中 传输数据时用到的 stringify 也是在 html 定义好的,可以直接使用,用 JSON.stringify 会报错, 原因
  • 如果使用的是 react-native-webview,在 tooltip 中的 postMessage 应该为 window.ReactNativeWebView.postMessage