برنامه نویسی

Master-Detail React DataGrid با نمودارها

در این مقاله، می‌خواهم به شما نشان دهم که چقدر آسان است که از پشتیبانی جزئیات اصلی در Infinite Table React DataGrid برای جابه‌جایی بین جدول و نمای نمودار در جزئیات ردیف استفاده کنید.

در مولفه RowDetail جدول بی نهایت، a را ارائه می کنیم ، که به نوبه خود هر یک را رندر می کند جزء یا نمودار

را در InfiniteTable بسیار قدرتمند است و تمام داده های مورد نیاز شبکه را پردازش می کند. همه گروه بندی ردیف، مرتب سازی، فیلتر کردن، تجمع، محوری در انجام می شود – بنابراین می توانید از آن به صورت مستقل یا با InfiniteTable استفاده کنید – کاملاً به شما بستگی دارد.

در عمل، این بدان معنی است که شما می توانید از آن استفاده کنید برای پردازش داده‌های شما و سپس به سادگی آن را به یک کتابخانه نمودار ارسال کنید ag-charts-react.

const detailGroupBy: DataSourcePropGroupBy<Developer> = [{ field: "stack" }];
const detailAggregationReducers: DataSourcePropAggregationReducers<Developer> =
  {
    salary: {
      field: "salary",
      initialValue: 0,
      reducer: (acc, value) => acc + value,
      done: (value, arr) => Math.round(arr.length ? value / arr.length : 0),
    },
  };

function RowDetail() {
  const rowInfo = useMasterRowInfo<City>()!;
  const [showChart, setShowChart] = React.useState(rowInfo.id % 2 == 1);

  return (
    <div style={{...}}>
      <button onClick={() => setShowChart((showChart) => !showChart)}>
        Click to see {showChart ? "grid" : "chart"}
      button>

      {/**
       * In this example, we leverage the DataSource aggregation and grouping feature to
       * calculate the average salary by stack for the selected city.
       */}
      <DataSource<Developer>
        data={detailDataSource}
        primaryKey="id"
        groupBy={detailGroupBy}
        aggregationReducers={detailAggregationReducers}
      >
        {/**
         * Notice here we're not rendering an InfiniteTable component
         * but rather we use a render function to access the aggregated data.
         */}
        {(params) => {
          // here we decide if we need to show the chart or the grid
          if (!showChart) {
            return (
              <InfiniteTable
                columns={detailColumns}
                domProps={{
                  style: { paddingTop: 30 },
                }}
              />
            );
          }

          // the dataArray has all the aggregations and groupings done for us, 
          // so we need to retrieve the correct rows and pass it to the charting library
          const groups = params.dataArray.filter((rowInfo) => rowInfo.isGroupRow);
          const groupData = groups.map((group) => ({ stack: group.data?.stack, avgSalary: group.reducerData?.salary }));

          return (
            <AgChartsReact
              options={{
                autoSize: true,
                title: {
                  text: `Avg salary by stack in ${rowInfo.data?.name}, ${rowInfo.data?.country}`,
                },
                data: groupData,
                series: [
                  {
                    type: "bar",
                    xKey: "stack",
                    yKey: "avgSalary",
                    yName: "Average Salary",
                  },
                ],
              }}
            />
          );
        }}
      </DataSource>
    div>
  );
}
وارد حالت تمام صفحه شوید

از حالت تمام صفحه خارج شوید

نسخه ی نمایشی بالا با استفاده از ag-charts-react بسته برای ارائه نمودارها

درباره رندر کردن محتوای سفارشی در تنظیم جزئیات بیشتر بخوانید.

نوشته های مشابه

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

دکمه بازگشت به بالا