• Demos
    • JavaScript Charts
    • JavaScript StockCharts
  • Download/NPM
    • Download CanvasJS
    • Install via NPM
  • Integrations
    Front End Technology Samples
    • React Charts
    • Angular Charts
    • Vue.js Charts New!
    • jQuery Charts
    • Dashboards
    Server Side Technology Samples
    • PHP Charts
    • Python Charts New!
    • ASP.NET MVC Charts
    • Spring MVC Charts
    • JSP Charts
  • License
  • Blog
  • Docs
    • Chart Documentation
    • StockChart Documentation
  • Support Forum
    • Chart Support
    • StockChart Support
  • My Account
My Account
  • KEY FEATURES
    • Chart with Index / Data Label
    • Chart with Zooming / Panning
    • Chart using JSON Data
    • Chart with Animation
    • Multi Series Chart
    • Chart with Multiple Axes
    • Chart with Crosshair
    • Chart with Scale Breaks
    • Chart with Logarithmic Axis
    • Performance with 50,000 Data Points
    • Responsive Charts
    • Chart with Drilldown
  • LINE CHARTS
    • Line Chart with Formatted Axes Labels
    • Dashed Line Chart
    • Multi Series Line Chart
    • Spline Chart
    • Multi Series Spline Chart
    • Step Line Chart
    • Multi Series Step Line Chart
  • BAR CHARTS
    • Bar Chart with Category Axis
    • Multi Series Bar Chart
    • Stacked Bar Chart
    • Stacked Bar 100% Chart
    • Stacked Bar 100% Chart with Indexlabel
  • COLUMN CHARTS
    • Column Chart with Category Axis
    • Multi Series Column Chart
    • Stacked Column Chart
    • Stacked Column 100% Chart
    • Stacked Column 100% Chart with Indexlabel
    • Waterfall Chart
    • Waterfall Chart with Indexlabel
  • AREA CHARTS
    • Area Chart with Date-Time Axis
    • Multi Series Area Chart
    • Spline Area Chart
    • Multi Series Spline Area Chart
    • Step Area Chart
    • Stacked Area Chart
    • Stacked Area 100% Chart
  • RANGE CHARTS
    • Range Column Chart
    • Multi Series Range Column Chart
    • Range Bar Chart
    • Multi Series Range Bar Chart
    • Range Area Chart
    • Multi Series Range Area Chart
    • Range Spline Area Chart
    • Multi Series Range Spline Area Chart
  • PIE & DOUGHNUT CHARTS
    • Pie Chart with Indexlabels
    • Pie Chart with Index Labels Placed Inside
    • Doughnut Chart
    • Doughnut Chart in Dark Theme
  • FUNNEL & PYRAMID CHARTS
    • Funnel Chart
    • Funnel Chart with Custom Neck
    • Pyramid Chart
  • FINANCIAL CHARTS
    • Candlestick Chart
    • Candlestick Chart from JSON
    • OHLC Chart
    • OHLC with Trendline
    • Box and Whisker Chart
    • Box and Whisker Chart with Customization
  • BUBBLE & SCATTER CHARTS
    • Scatter Chart
    • Scatter Chart with Custom Markers
    • Bubble Chart
  • COMBINATION CHARTS
    • Error chart
    • Error Line Chart
    • Pareto Chart
    • Combination of Column, Line and Area Chart
  • DYNAMIC CHARTS
    • Dynamic Line Chart
    • Dynamic Column Chart
    • Dynamic Multi Series Chart
  • STOCKCHARTS
    • StockChart with Numeric Axis
    • StockChart with Date-Time Axis
    • StockChart with SplineArea & Range Selector
  • ANGULAR, VUE.JS, JAVASCRIPT, JQUERY
    • Angular Charts
    • Vue.js Charts
    • jQuery Charts
    • JavaScript Charts
  • SERVER SIDE TECHNOLOGIES
    • ASP.NET MVC Charts
    • PHP Charts
    • Python Charts
    • JSP Charts
    • Spring MVC Charts

React OHLC Chart with Trend Line

Download React Chart Samples
  • React Chart Samples
  • JavaScript Chart Samples
  • Angular Chart Samples
  • Vue.js Chart Samples
  • jQuery Chart Samples
  • PHP Chart Samples
  • Python Django Chart Samples
  • ASP.NET Chart Samples
  • JSP Chart Samples
  • Spring MVC Chart Samples
  • Dashboard Samples
  • JavaScript StockChart Samples

Example shows React OHLC Chart Combined with Line Chart showing trend. Data is fetched from JSON using AJAX.

  • React Code
/* App.js */
import React, { Component } from 'react';
import CanvasJSReact from '@canvasjs/react-charts';
//let CanvasJSReact = require('@canvasjs/react-charts');

let CanvasJS = CanvasJSReact.CanvasJS;
let CanvasJSChart = CanvasJSReact.CanvasJSChart;

class App extends Component {
    constructor() {
        super();
        this.state = { dps1: [], isLoaded: false };
        this.calculateTrendLine = this.calculateTrendLine.bind(this);
    }

    componentDidMount() {
        fetch('https://canvasjs.com/data/docs/btcusd2018.json')
            .then(res => res.json())
            .then((data) => {
                let dps = [];
                for (let i = 0; i < data.length; i++) {
                    dps.push({ x: new Date(data[i].date), y: [Number(data[i].open), Number(data[i].high), Number(data[i].low), Number(data[i].close)] });
                }

                this.setState({
                    isLoaded: true,
                    dps1: dps
                }, () => {
                    if (this.chart.data.length === 1)
                        this.calculateTrendLine();
                });
            });
    }

    calculateTrendLine() {
        let a, b, c, d, e, slope, yIntercept;
        let chart = this.chart;
        let xSum = 0, ySum = 0, xySum = 0, xSquare = 0, dpsLength = chart.data[0].dataPoints.length;

        for (let i = 0; i < dpsLength; i++)
            xySum += (chart.data[0].dataPoints[i].x.getTime() * chart.data[0].dataPoints[i].y[3])
        a = xySum * dpsLength;

        for (let i = 0; i < dpsLength; i++) {
            xSum += chart.data[0].dataPoints[i].x.getTime();
            ySum += chart.data[0].dataPoints[i].y[3];
        }
        b = xSum * ySum;

        for (let i = 0; i < dpsLength; i++)
            xSquare += Math.pow(chart.data[0].dataPoints[i].x.getTime(), 2);
        c = dpsLength * xSquare;

        d = Math.pow(xSum, 2);
        slope = (a - b) / (c - d);
        e = slope * xSum;
        yIntercept = (ySum - e) / dpsLength;

        let startPoint = this.getTrendLinePoint(chart.data[0].dataPoints[0].x, slope, yIntercept);
        let endPoint = this.getTrendLinePoint(chart.data[0].dataPoints[dpsLength - 1].x, slope, yIntercept);

        this.chart.addTo("data", {
            type: "line", //Line series showing trend
            markerSize: 0,
            name: "Trend",
            showInLegend: true,
            color: "#E64A19",
            yValueFormatString: "$#,###.00",
            dataPoints: [startPoint, endPoint]
        });
    }

    getTrendLinePoint(x, slope, intercept) {
        return { x: x, y: ((slope * x) + intercept) };
    }

    render() {
        const options = {
            theme: "light2",
            zoomEnabled: true,
            title: {
                text: "Bitcoin Price"
            },
            axisY: {
                title: "Price in USD",
                prefix: "$"
            },
            axisX: {
                valueFormatString: "MMM",
            },
            legend: {
                verticalAlign: "bottom",
                cursor: "pointer",
                itemclick: function (e) {
                    if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
                        e.dataSeries.visible = false;
                    } else {
                        e.dataSeries.visible = true;
                    }
                    e.chart.render();
                }
            },
            toolTip: {
                shared: true
            },
            data: [{
                type: "ohlc",
                showInLegend: true,
                name: "Bitcoin Price",
                yValueFormatString: "$#,###.00",
                xValueType: "dateTime",
                dataPoints: this.state.dps1
            }]
        };

        return (
            <div>
                {
                    this.state.isLoaded && <CanvasJSChart options={options}
                        onRef={ref => this.chart = ref}
                    />
                }
            </div>
        );
    }
}
export default App;                              

Chart Customizations

You can control the visibility of a dataseries by setting visible property. Other frequently used customization options are color, showInLegend, legendText etc.

Note   For step by step instructions, follow our React Integration Tutorial

Quick Links

  • Chart Docs
  • StockChart Docs
  • About Us
  • FAQs

Server Side Technologies

  • ASP.NET MVC Charts
  • PHP Charts
  • JSP Charts
  • Spring MVC Charts

Front Side Technologies

  • JavaScript Charts
  • jQuery Charts
  • React Charts
  • Angular Charts
  • JavaScript StockCharts

Contact

  • Fenopix, Inc.
  • 2093 Philadelphia Pike,
  • #5678, Claymont,
  • Delaware 19703
  • United States Of America

©2025 Fenopix Privacy Policy Cookies Policy Careers