รายละเอียดสำหรับเรียนรู้ React ปี 2020-2023 โดยโค้ชพล ดูหลักสูตรได้ที่ https://www.nextflow.in.th/react-training
ถ้ายังไม่ได้ติดตั้ง package ชื่อ react-google-charts
ล่ะก็ อย่าลืมรันคำสั่งติดตั้งให้กับโปรเจคล่ะ
npm i react-google-charts
yarn add react-google-charts
สร้างไฟล์ src/components/StatChart.js
import React, { Component } from 'react'
export default class StatChart extends Component {
render() {
return (
<div>
Chart me
</div>
)
}
}
เปิดไฟล์ src/App.js
import StatChart
component เข้ามาใช้งาน
import StatChart from './components/StatChart';
render() {
//...
<Row gutter={16}>
<Col span={12}><MapBranch /></Col>
<Col span={12}><StatChart/></Col>
</Row>
//...
}
import React from 'react';
import logo from './logo.svg';
import './App.css';
import HeaderBar from './components/HeaderBar';
import { Layout, Menu, Row, Col } from 'antd';
import MapBranch from './components/MapBranch';
import StatChart from './components/StatChart';
const { Header, Content, Footer } = Layout;
function App() {
return (
<div>
<Layout className="layout">
<HeaderBar />
<Content style=>
<div
style=>
<Row gutter={16}>
<Col span={12}><MapBranch /></Col>
<Col span={12}><StatChart/></Col>
</Row>
</div>
</Content>
<Footer style=>React Redux Workshop ©2012-2019 Created by Nextflow.in.th</Footer>
</Layout>,
</div>
);
}
export default App;
เปิดไฟล์ src/components/StatChart.js
และใช้ Card
component ของ AntDesign
import React, {Component} from 'react'
import {Card} from 'antd';
export default class StatChart extends Component {
render() {
return (
<div>
<Card title="Chart" style=>
<p>Card content</p>
</Card>
</div>
)
}
}
Documentation: react-google-charts
import React, { Component } from 'react'
import { Card } from 'antd';
import Chart from 'react-google-charts';
export default class StatChart extends Component {
render() {
return (
<div>
<Card title="Chart" style=>
<Chart
width={'100%'}
height={'400px'}
chartType="LineChart"
loader={<div>Loading Chart</div>}
data={[
['x', 'dogs'],
[0, 0],
[1, 10],
[2, 23],
[3, 17],
[4, 18],
[5, 9],
[6, 11],
[7, 27],
[8, 33],
[9, 40],
[10, 32],
[11, 35],
]}
rootProps=data-testid
/>
</Card>
</div>
)
}
}
ทดสองแก้ไข props chartType
เป็น Line
<Chart
width={'100%'}
height={'400px'}
chartType="Line"
>