创建react项目

react官网

https://reactjs.bootcss.com/

创建react项目

npx create-react-app my-app

cd my-app
npm start

使用vite创建react项目

npm install -g vite

使用npm  npm create vite@latest

使用yarn yarn create vite

使用pnpm create vite

vite官网

 创建组件,jsx

如果你书写的标签代码与 return 语句不在同一行上,则必须将其用一对圆括号括起来

export default function Profile() {
  return (
      <img
          src="https://i.imgur.com/MK3eW3Am.jpg"
          alt="Katherine Johnson"
      />
  )
}

 全局安装react

npm i create-react-app -g
然后使用create-react-app 项目名称创建react项目

react命名规范

驼峰命名法

大驼峰:首字母小写,其余每一个有意义的单词首字母大写

小驼峰:PascalCase 首字母都要大写

kabab-case写法:personal-box

添加样式的时候,使用的不在是class了用的是className

给react安装一个echarts的插件,然后写一个小的案例

/**
 * 使用echarts写一个折线图,折线图有平均线超出平均线添加一个红色的图标,低于平均线添加一个蓝色的图标
 */
import React, {useEffect, useRef} from "react";
import * as echarts from "echarts";
// function App() {
//     return (
//        <>
//            <div className="App">
//                <ColoredChart data={[150, 230, 224, 218, 135, 147, 260]} />
//            </div>
//        </>
//     );
// }
const ColoredChart = ({data}) => {
    const chartRef = useRef(null);
    useEffect(() => {
        const chart = echarts.init(chartRef.current);
        // 计算平均值
        // 将超过平均值的部分设为红色,低于平均值的部分设为蓝色
        // 每个节点都添加上数据
        const option = {
            title: {
                text: '未来一周气温变化',
                subtext: '纯属虚构'
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: ['最高气温', '最低气温', '适中气温']
            },
            toolbox: {
                show: true,
                feature: {
                    dataZoom: {
                        yAxisIndex: 'none'
                    },
                    dataView: {readOnly: false},
                    magicType: {type: ['line', 'bar']},
                    restore: {},
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    formatter: '{value}'
                }
            },
            series: [
                {
                    name: '最高气温',
                    type: 'line',
                    data: [10, 11, 13, 11, 12, 12, 9],
                    markPoint: {
                        data: [
                            {type: 'max', name: '最大值', background: 'red'},
                            {type: 'min', name: '最小值', background: 'blue'}
                        ]
                    },
                    markLine: {
                        data: [
                            {type: 'average', name: '平均值'}
                        ]
                    }
                },
                {
                    name: '最低气温',
                    type: 'line',
                    data: [1, -2, 2, 5, 3, 2, 0],
                    markPoint: {
                        data: [
                            {type: 'max', name: '最大值', background: 'red'},
                            {type: 'min', name: '最小值', background: 'blue'}
                        ]
                    },
                    markLine: {
                        data: [
                            {type: 'average', name: '平均值'},
                            [{
                                symbol: 'none',
                                x: '90%',
                                yAxis: 'max'
                            }, {
                                symbol: 'circle',
                                label: {
                                    position: 'start',
                                    formatter: '最大值'
                                },
                                type: 'max',
                                name: '最高点'
                            }]
                        ]
                    }
                },
                {
                    name: '适中气温',
                    type: 'line',
                    data: [32, 0, 2, 5, 3, 2, 0],
                    markPoint: {
                        data: [
                            {type: 'max', name: '最大值'},
                            {type: 'min', name: '最小值'}
                        ]
                    },
                    markLine: {
                        data: [
                            {type: 'average', name: '平均值'},
                            [{
                                symbol: 'none',
                                x: '90%',
                                yAxis: 'max'
                            }, {
                                symbol: 'circle',
                                label: {
                                    position: 'start',
                                    formatter: '最大值'
                                },
                                type: 'max',
                                name: '最高点'
                            }]
                        ]
                    }
                }
            ]
        };
        chart.setOption(option);
        return () => {
            chart.dispose();
        };
    }, [data]);
    return <div ref={chartRef} style={{height: "400px"}}/>;
}
// export default App;
/**
 * 使用echarts写一个折线图,所有数据都展示在节点上,折线图有平均线超出平均线添加一个红色的图标,低于平均线添加一个蓝色的图标
 */
let text: String = 'hello react';
let text2: Boolean = false;
let obj: Object = {name: 'zhangsan', age: 18};
let arr: Array<Number> = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let arr2: Array<Object> = [{name: 'zhangsan', age: 18}, {name: 'lisi', age: 19}];

function App() {
    return (
        <>
            {text2 ? text : text + '1'}
            {obj.name}
            <div className="box" style={{color: 'red',}}>
                {arr.map((item, index) => {
                    return <span key={index}>{item}</span>
                })}
            </div>
            {arr2.map((item, index) => {
                return <span key={index}>{item.name}</span>
            })}
            <div className="App">
                <ColoredChart data={[150, 230, 224, 218, 135, 147, 260]}/>
            </div>
        </>
    );
}

export default App;

把echarts当作组件拆分出来

/**
 * 使用echarts写一个折线图,折线图有平均线超出平均线添加一个红色的图标,低于平均线添加一个蓝色的图标
 */
import React, {useEffect, useRef} from "react";
import * as echarts from "echarts";

const ColoredChart = ({data}) => {
    const chartRef = useRef(null);
    useEffect(() => {
        const chart = echarts.init(chartRef.current);
        // 计算平均值
        // 将超过平均值的部分设为红色,低于平均值的部分设为蓝色
        // 每个节点都添加上数据
        const option = {
            title: {
                text: '未来一周气温变化',
                subtext: '纯属虚构'
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: ['最高气温', '最低气温', '适中气温']
            },
            toolbox: {
                show: true,
                feature: {
                    dataZoom: {
                        yAxisIndex: 'none'
                    },
                    dataView: {readOnly: false},
                    magicType: {type: ['line', 'bar']},
                    restore: {},
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    formatter: '{value}'
                }
            },
            series: [
                {
                    name: '最高气温',
                    type: 'line',
                    data: [10, 11, 13, 11, 12, 12, 9],
                    markPoint: {
                        data: [
                            {type: 'max', name: '最大值', background: 'red'},
                            {type: 'min', name: '最小值', background: 'blue'}
                        ]
                    },
                    markLine: {
                        data: [
                            {type: 'average', name: '平均值'}
                        ]
                    }
                },
                {
                    name: '最低气温',
                    type: 'line',
                    data: [1, -2, 2, 5, 3, 2, 0],
                    markPoint: {
                        data: [
                            {type: 'max', name: '最大值', background: 'red'},
                            {type: 'min', name: '最小值', background: 'blue'}
                        ]
                    },
                    markLine: {
                        data: [
                            {type: 'average', name: '平均值'},
                            [{
                                symbol: 'none',
                                x: '90%',
                                yAxis: 'max'
                            }, {
                                symbol: 'circle',
                                label: {
                                    position: 'start',
                                    formatter: '最大值'
                                },
                                type: 'max',
                                name: '最高点'
                            }]
                        ]
                    }
                },
                {
                    name: '适中气温',
                    type: 'line',
                    data: [32, 0, 2, 5, 3, 2, 0],
                    markPoint: {
                        data: [
                            {type: 'max', name: '最大值'},
                            {type: 'min', name: '最小值'}
                        ]
                    },
                    markLine: {
                        data: [
                            {type: 'average', name: '平均值'},
                            [{
                                symbol: 'none',
                                x: '90%',
                                yAxis: 'max'
                            }, {
                                symbol: 'circle',
                                label: {
                                    position: 'start',
                                    formatter: '最大值'
                                },
                                type: 'max',
                                name: '最高点'
                            }]
                        ]
                    }
                }
            ]
        };
        chart.setOption(option);
        return () => {
            chart.dispose();
        };
    }, [data]);
    return <div ref={chartRef} style={{height: "400px"}}/>;
}
export default ColoredChart;

然后再另外一个组件中进行引入

/**
 * 使用echarts写一个折线图,所有数据都展示在节点上,折线图有平均线超出平均线添加一个红色的图标,低于平均线添加一个蓝色的图标
 */
import React from "react";
import ColoredChart from './Ech';

let text: String = 'hello react';
let text2: Boolean = false;
let obj: Object = {name: 'zhangsan', age: 18};
let arr: Array<Number> = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let arr2: Array<Object> = [{name: 'zhangsan', age: 18}, {name: 'lisi', age: 19}];

function App() {
    return (
        <>
            {text2 ? text : text + '1'}
            {obj.name}
            <div className="box" style={{color: 'red',}}>
                {arr.map((item, index) => {
                    return <span key={index}>{item}</span>
                })}
            </div>
            {arr2.map((item, index) => {
                return <span key={index}>{item.name}</span>
            })}
            <div className="App">
                <ColoredChart/>
            </div>
        </>
    );
}

export default App;

 

posted @ 2023-06-08 09:44  帅气丶汪星人  阅读(68)  评论(0)    收藏  举报