简体   繁体   中英

Curved lines are not displayed with react-chartjs-2

I'm using react-chart.js 2 to display continuous data in scatter chart form. I wanted to put options on the charts but some of them do not work properly or at all. So I have problems with these following option: the lines are not curved between the points and there are straight.

The version of react-chart.js 2 installed is: 5.2.0 and the version of chartJS is: 4.1.2.

What i have tried :

I have tried to modify the parameter in options for curve ( elements: {line: {tension: 0.1,spanGaps: false}}) but it didn't work.

Here is the code:

App.js :

import React, { useState, useEffect, useRef, useSyncExternalStore } from 'react';
import Modal from './Modal/Modal'
import {Chart as ChartJS,LinearScale,PointElement,LineElement,Tooltip,Legend,Title,scales} from 'chart.js';
import {Scatter } from 'react-chartjs-2';
ChartJS.register(
    LinearScale,
    PointElement,
    LineElement,
    Tooltip,
    Legend,
    Title);

//JSON.parse(event.data)
//<Doughnut ref={chartReference} data={data} />
export const options1 = {
    responsive: true,
    maintainAspectRatio:false,
    plugins: {
      legend: false,
      title: {
        display:true,
        text: 'TWIST',
      },
    },
  };


export default function App() {
  let da;
  const [data, setData] = useState(null);

  const [chartData1,setChartData1]=useState({
    datasets: [
        {
          label: 'Twist',
          showLine:true,
          maintainAspectRatio:false,
          fill:false,
          data: [{x:3,y:1},{x:3.5,y:2},{x:5.5,y:3},{x:5.25,y:4},{x:5,y:5}],
          backgroundColor: '#df9305',
          borderColor: '#df9305'
        }]
    });

    const [show,setShow] = useState(false);

  const chart1=(d) =>{
    setChartData1({
        datasets: [
            {
              label: 'Twist',
              showLine:true,
              fill:false,
              data: d,
              backgroundColor: '#df9305',
              borderColor: '#df9305'
            }]
    });
  };

  console.log(data)
  useEffect(() => {
    const socket = new WebSocket('ws://localhost:8000');

    socket.addEventListener('message', (event) => {
      setData(JSON.parse(event.data));
      da=[{x:JSON.parse(event.data)["fs"]["s1"]["twist"],y:1},
      {x:JSON.parse(event.data)["fs"]["s2"]["twist"],y:2},
      {x:JSON.parse(event.data)["fs"]["s3"]["twist"],y:3},
      {x:JSON.parse(event.data)["fs"]["s4"]["twist"],y:4},
      {x:JSON.parse(event.data)["fs"]["s5"]["twist"],y:5}];
      chart1(da);
    });

  }, []);
  
  
  return (
        <div>
            <div className="home">
                <div className="template-1" id="temp1">
                <div class="panel-3">
                    <div class="panel-header">
                    <h1>Courbes</h1>
                    <i class='bx bx-cog modal-trigger-panel'></i>
                    </div>
                    <div class="panel-body" >
                    <div class="graph-container" style={{display: "flex", flexDirection: "row",height: "100%",width:"100%"}}>
                        <div style={{width: "25%",height: "100%"}}>
                            <Scatter options={options1} data={chartData1} />
                        </div>
                        </div>
                    </div>
                    </div>
                </div>
                </div>
                <Modal onClose={() => setShow(false)} show={show} />
            </div>
  );
}

Regards,

Okay this one took a while but I figured it out. I have solved it without tree shaking so that is what I will be showing, you can of course add tree shaking but I think that perhaps that is part of what is giving you trouble. To load it lazily just do

import 'chart.js/auto';
import { Scatter } from 'react-chartjs-2';

And then as I'm sure you've tried add this to options

elements: {
    line: {
        tension: 0.5,
    },
},

If you require tree-shaking be free to leave a comment and I'll try and figure out how to make that work as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM