如何在React中使用Material UI和DevExpress建立條形圖?


Material UI是一個流行的CSS庫,我們可以用它來為React應用程式設定樣式。它包含各種預設樣式的React元件,我們可以透過將它們匯入程式碼中直接在應用程式中使用。

“dx-react-chart-material-ui”是DevExpress的NPM包,可以連線Material UI和DevExpress的“dx-react-chart”庫。“dx-react-chart”用於建立圖表,而Material UI用於設定圖表的樣式。

使用者可以執行以下命令在React應用程式中安裝Material UI。

npm install @mui/material @emotion/react @emotion/styled

同樣,執行以下命令來安裝DevExpress NPM包。

npm i @devexpress/dx-react-chart

語法

使用者可以按照以下語法使用DevExpress建立條形圖。

<Chart data = {data}>
   <BarSeries valueField = "price" argumentField = "fruit" />
   <Title text = "Fruit price" />
</Chart>

在上面的語法中,我們使用了DevExpress的“Chart”、“BarSeries”和“Title”元件。“Chart”元件顯示圖表,“BarSeries”元件顯示條形圖,“Title”元件顯示標題。

示例1(簡單條形圖)

在下面的示例中,我們從Material UI匯入了“Paper”元件。我們還從“devexpress”NPM包中匯入了所需的元件。

我們還定義了包含圖表資料的data[]陣列。它包含水果名稱及其價格。我們建立一個簡單的條形圖來比較水果價格。在輸出中,使用者可以看到條形圖。

import React, { useState } from "react";
import Paper from "@mui/material/Paper";
import {
   Chart,
   BarSeries,
   Title,
   ArgumentAxis,
   ValueAxis,
} from "@devexpress/dx-react-chart-material-ui";
import { Animation } from "@devexpress/dx-react-chart";

const data = [
   { fruit: "Apple", price: 150 },
   { fruit: "Orange", price: 250 },
   { fruit: "Banana", price: 100 },
   { fruit: "Mango", price: 200 },
   { fruit: "Grapes", price: 50 },
   { fruit: "Pineapple", price: 90 },
   { fruit: "Watermelon", price: 170 },
   { fruit: "Papaya", price: 120 },
   { fruit: "Guava", price: 80 },
];

function App() {
   return (
      <div>
         <h2>
            Creating the{" "}
            bar chart using the <i>  devexpress NPM   package and material UI </i>
         </h2>
         <Paper>
            <Chart data = {data}>
               <ArgumentAxis />
               <ValueAxis max = {200} />
               <BarSeries valueField = "price" argumentField = "fruit" />
               <Title text = "Fruit Price" />
               <Animation />
            </Chart>
         </Paper>
      </div>
   );
}
export default App;

輸出

示例2(並排條形圖)

在下面的示例中,我們演示瞭如何建立一個並排條形圖。資料包含材料名稱以及根據顏色的價格。

圖表包含針對每種材料的3個條形圖,每個條形圖代表不同的顏色。我們使用了“Barseries”元件為每種材料建立一個條形圖。我們還為元件設定了標題。

在輸出中,使用者可以看到並排的條形圖,每個條形圖根據顏色比較不同材料的價格。

import React from "react";
import Paper from "@mui/material/Paper";
import {
   Chart,
   BarSeries,
   Title,
   ArgumentAxis,
   ValueAxis,
   Legend,
} from "@devexpress/dx-react-chart-material-ui";
import { Stack, Animation } from "@devexpress/dx-react-chart";

const chartData = [
   { material: "Aluminium", yellow: 3000, silver: 3200, grey: 2900 },
   { material: "Copper", yellow: 2300, silver: 2700, grey: 1900 },
   { material: "Steel", yellow: 1400, silver: 2100, grey: 1700 },
   { material: "Iron", yellow: 2200, silver: 1700, grey: 2800 },
];

function App() {
  return (
      <div>
      <h2>
         Creating the{" "}
         stacked bar chart using the <i> devexpress NPM package and material UI </i>
      </h2>
      <Paper>
         <Chart data = {chartData}>
            <ArgumentAxis />
            <ValueAxis />

            <BarSeries
               Name = "yellow color"
               valueField = "yellow"
               argumentField = "material"
               color = "#ffd700"
            />
            <BarSeries
               Name = "Silver color"
               valueField = "silver"
               argumentField = "material"
               color = "#c0c0c0"
            />
            <BarSeries
               Name = "grey color"
               valueField = "grey"
               argumentField = "material"
               color = "grey"
            />
            <Animation />
            <Legend position = "bottom" />
            <Title text = "Price of Materials" />
            <Stack />
         </Chart>
         </Paper>
      </div>
   );
}
export default App;

輸出

示例3(堆疊條形圖)

在下面的示例中,我們演示瞭如何建立一個堆疊條形圖。我們準備了按州劃分的居民人口、車輛、房屋和商店資料來建立條形圖。

在下面的示例中,我們演示瞭如何建立一個堆疊條形圖。我們準備了按州劃分的居民人口、車輛、房屋和商店資料來建立條形圖。

import React from "react";
import Paper from "@mui/material/Paper";
import {
   Chart,
   BarSeries,
   Title,
   ArgumentAxis,
   ValueAxis,
   Legend,
} from "@devexpress/dx-react-chart-material-ui";
import { Stack, Animation } from "@devexpress/dx-react-chart";

const chartData = [
   { state: "Gujarat", population: 3938223, vehicles: 3456800, houses: 2535447, shops: 454464 },
   { state: "Maharashtra", population: 2446456, vehicles: 3864500, houses: 6485534, shops: 344654 },
   { state: "Rajasthan", population: 2332543, vehicles: 4756549, houses: 981496, shops: 545621 },
   { state: "Punjab", population: 3434657, vehicles: 5686564, houses: 4569847, shops: 448734 },
];

function App() {
   return (
      <div>
         <h2>
            Creating the{" "}
            <i>
               Stacked bar chart using the devexpress NPM package and Material UI.
            </i>
         </h2>
         <Paper>
            <Chart data = {chartData}>
               <ArgumentAxis />
               <ValueAxis max = {50000000} />

               <BarSeries
                  name = "Population"
                  valueField = "population"
                  argumentField = "state"
                  color = "#8884d8"
               />
               <BarSeries
                  name = "Vehicles"
                  valueField = "vehicles"
                  argumentField = "state"
                  color = "#82ca9d"
               />
               <BarSeries
                  name = "Houses"
                  valueField = "houses"
                  argumentField = "state"
                  color = "#ffc658"
               />
               <BarSeries
                  name = "Shops"
                  valueField = "shops"
                  argumentField = "state"
                  color = "#ff7f50"
               />
               <Animation />
               <Legend position = "bottom" />
               <Title text = "State-wise Data" />
               <Stack stacks = {[{ series: ["Population", "Vehicles", "Houses", "Shops"] }]} />
            </Chart>
         </Paper>
      </div>
   );
}

export default App;

輸出

我們學習瞭如何使用DevExpress和Material UI庫來建立和設計圖表。DevExpress NPM包是Material UI和DevExpress圖表庫之間的橋樑。此外,我們還學習了在本教程中建立各種型別的條形圖。

更新於:2023年7月14日

863 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告