Today I have learned Line chart in javascript

 Line chart in javascript:

Chart.js is an free JavaScript library for making HTML-based charts. It is one of the simplest visualization libraries for JavaScript, and comes with the many built-in chart types:

  • Scatter Plot
  • Line Chart
  • Bar Chart
  • Pie Chart
  • Donut Chart
  • Bubble Chart
  • Area Chart
  • Radar Chart
  • Mixed Chart
  • A line chart is a way of plotting data points on a line. Often, it is used to show trend data, or the comparison of two data sets.
  • The following interpolation modes are supported.

    • 'default'
    • 'monotone'

    The 'default' algorithm uses a custom weighted cubic interpolation, which produces pleasant curves for all types of datasets.

    The 'monotone' algorithm is more suited to y = f(x) datasets: it preserves monotonicity (or piecewise monotonicity) of the dataset being interpolated, and ensures local extremums (if any) stay at input data points.

    If left untouched (undefined), the global options.elements.line.cubicInterpolationMode property is used.

    #Segment

    Line segment styles can be overridden by scriptable options in the segment object. Currently, all of the border* and backgroundColor options are supported. The segment styles are resolved for each section of the line between each point. undefined fallbacks to main line styles.

  • data: [65, 59, 80, 81, 56, 55, 40],
            labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
          };
        }
     
        componentDidMount() {
          this.drawChart();
        }
     
        componentDidUpdate() {
          this.drawChart();
        }
      drawChart() {
          const canvas = this.refs.canvas;
          const ctx = canvas.getContext('2d');
     
          // Clear the canvas
          ctx.clearRect(0, 0, canvas.width, canvas.height);
     
          // Draw the line chart
          ctx.beginPath();
          ctx.moveTo(50, 250); // Move to the starting point
     
          const data = this.state.data;
          const labels = this.state.labels;
          const dataLength = data.length;
     
      const interval = (canvas.width - 100) / (dataLength - 1);
     
          for (let i = 0; i < dataLength; i++) {
            const x = 50 + i * interval;
            const y = 250 - (data[i] * 2); // Adjusting scale for simplicity
     
            ctx.lineTo(x, y);
            ctx.arc(x, y, 3, 0, 2 * Math.PI); // Draw a small circle at each data point
            ctx.fillText(labels[i], x, 270); // Display labels
          }
     
          ctx.stroke();
        }
      render() {
          return (
            <div className="chart-container">
              <canvas ref="canvas" width={600} height={300}></canvas>
            </div>
          );
  •                   Output:

Comments

Popular posts from this blog

Building a Full-Stack Student Management System with React.js and NestJS

Today I have solved the problem in javascript

Today I have solved problem in javascript