react-chartjs-2#ChartData TypeScript Examples

The following examples show how to use react-chartjs-2#ChartData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CompareChart.tsx    From SeeQR with MIT License 5 votes vote down vote up
getChartData = (
  queries: AppState['queries']
): ChartData<Chart.ChartData> => {
  /**
   * Gets next color from defined pallete.
   */
  const getColor = (() => {
    let nextColor = 0;
    return () => {
      // cycle through colors
      const color = compareChartColors[nextColor % compareChartColors.length];
      nextColor += 1;
      return color;
    };
  })();

 

  const comparedQueries = Object.values(queries);

  // unique query labels
  const uniqueLabels = [...new Set(comparedQueries.map((query) => `label:${query.label} db:${query.db} group:${query.group}`))];
  const labels = [...new Set(comparedQueries.map((query) => query.group))];

  // unique dbs in comparison
  const comparedDbs = [...new Set(comparedQueries.map((query) => query.db))];

  // Algorithm for grouping speeds by group
  const groups:object = {};
  for (let i = 0; i < uniqueLabels.length; i++) {
    if (groups[queries[uniqueLabels[i]].db]) {
      groups[queries[uniqueLabels[i]].db].push(uniqueLabels[i]);
    } else {
      groups[queries[uniqueLabels[i]].db] = [uniqueLabels[i]];
    };
  };
  // array of objects representing each database that is being displayed
  const datasets = comparedDbs.map((db) => {
    const color = getColor();
    return {
      label: db,
      backgroundColor: color,
      borderColor: color,
      borderWidth: 1,
      // array with values for each group. If group doesn't have a query with a
      // given label being compared, set it's value to
      data: groups[db].map((label) => getTotalTime(queries[label])),
    };
  });
  return { labels, datasets };
}