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 toy = 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 globaloptions.elements.line.cubicInterpolationModeproperty is used.#Segment
Line segment styles can be overridden by scriptable options in the
segmentobject. Currently, all of theborder*andbackgroundColoroptions are supported. The segment styles are resolved for each section of the line between each point.undefinedfallbacks 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 canvasctx.clearRect(0, 0, canvas.width, canvas.height);// Draw the line chartctx.beginPath();ctx.moveTo(50, 250); // Move to the starting pointconst 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 simplicityctx.lineTo(x, y);ctx.arc(x, y, 3, 0, 2 * Math.PI); // Draw a small circle at each data pointctx.fillText(labels[i], x, 270); // Display labels}ctx.stroke();}render() {return (<div className="chart-container"><canvas ref="canvas" width={600} height={300}></canvas></div>);

Comments
Post a Comment