How to split a charts in react-charts-js-2 into two sets of datasets

91 views Asked by At

I was able to generate that split before a while back and I can't remember how I did it (hehe) I know how to plot the data for two sets of databases, but I am looking to split them visually so that they don't look connected

This is my component

import React from 'react';
import {
  Chart as ChartJS,
  CategoryScale,
  LinearScale,
  PointElement,
  LineElement,
  Title,
  Tooltip,
  Legend,
} from 'chart.js';
import { Line } from 'react-chartjs-2';
import faker from 'faker';

ChartJS.register(
  CategoryScale,
  LinearScale,
  PointElement,
  LineElement,
  Title,
  Tooltip,
  Legend
);

export const options = {
  responsive: true,
  plugins: {
    legend: {
      position: 'top' as const,
    },
    title: {
      display: true,
      text: 'Chart.js Line Chart',
    },
  },
};

const labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
const labels2 = ['1', '2', '3', '4', '5', '6', '7'];


export const data = {
  labels,
  datasets: [
    {
      label: 'Dataset 1',
      data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 })),
      borderColor: 'rgb(255, 99, 132)',
      backgroundColor: 'rgba(255, 99, 132, 0.5)',
    },
    {
      label: 'Dataset 2',
      data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 })),
      borderColor: 'rgb(53, 162, 235)',
      backgroundColor: 'rgba(53, 162, 235, 0.5)',
    },
  ],
};

export const data2 = {
  labels:[...labels, ...labels2],
  datasets: [
    {
      label: 'Dataset 1',
      data: [
        ...labels.map(() => faker.datatype.number({ min: -1000, max: 1000 })),
        ...labels2.map(() => faker.datatype.number({ min: -1000, max: 1000 }))
      ],
      borderColor: 'rgb(255, 99, 132)',
      backgroundColor: 'rgba(255, 99, 132, 0.5)',
      snapGap: true
    },
    {
      label: 'Dataset 2',
      data: [
        ...labels.map(() => faker.datatype.number({ min: -1000, max: 1000 })), 
        ...labels2.map(() => faker.datatype.number({ min: -1000, max: 1000 }))
      ],
      borderColor: 'rgb(53, 162, 235)',
      backgroundColor: 'rgba(53, 162, 235, 0.5)',
    },
  ],
};

export function App() {
  return <Line options={options} data={data2} />;
}

And you can play around with it CODESANDBOX

As you can see in my codesandbox, the datasets are jointed. Yes,. I know I concatenated them into one array, but I remember doing this for sure. In fact some of this code was copied from my other project where I successfully had them split inside the graph

Here's a screenshot of what I did once and hope to do again. <-- this link expires 1/31/2024 @11:59PM EST So, if you don't see it, that's why. Stack is not allowing images at the moment

Anyway, so, any idea on how to split the design? Adding hard-coded ghost values to the data arrays? CSS? JS?

I'm using Vite React and charts.js with react-charts-js-2

1

There are 1 answers

2
Scario Eva On

This is what you are looking for? we have 2 chart data data and data2 as mentioned in the code above. I'm returning parent div with 2 Line components that contain 2 different data.

export function App() {
  return <>
    <div style={{display:"inline}}>
     <Line options={options} data={data} />
    </div>
    <div style={{display:"inline}}>
     <Line options={options} data={data2} />
    </div>
  </>;
}