0

i am new in canvas.js and i am trying to plot graph dynamically but it is not working and give me error that chart.render() is not a function in console window. can anybody tell me my mistake? thanks in advance.

already tried this solution but i couldn't understand it. neither it resolves my error. canvasjs render is not a function

<!DOCTYPE html>
<html>
<head>
    <title>hello love</title>

</head>
<body><br />

    <script>

        function showchart() {
            var dataPoints = [];
            var dataPointsExpense = [];
            //calling function for y2 axis
            function addDataExpense(result) {

                for (var i = 0; i < result.length; i++) {
                    dataPointsExpense.push({
                        x: new Date(result[i].day + result[i].month + result[i].year),
                        y: result[i].ExpenseofMonth


                    });
                }

                 chart.render();

            }
            //calling finctions for income
            function addData(result) {
                console.log(result);
                for (var i = 0; i < result.length; i++) {
                    dataPoints.push({
                        x: new Date(result[i].day + result[i].month + result[i].year),
                        y: result[i].IncomeofMonth
                    }

                    );
                }
                 chart.render();

                addDataExpense(result)
            }


                    //canvas coding
                    var chart =  {

                        exportEnabled: true,
                        animationEnabled: true,
                        title: {
                            text: "Income Vs Expense of the Month"
                        },
                        subtitles: [{
                            text: "This will Show You Expense plus Income of the Month"
                        }],
                        axisX: {
                            title: "States"
                        },
                        axisY: {
                            title: "Income of the Month",
                            titleFontColor: "#4F81BC",
                            lineColor: "#4F81BC",
                            labelFontColor: "#4F81BC",
                            tickColor: "#4F81BC",
                            titleFontSize: 24,
                            includeZero: false
                        },
                        axisY2: {
                            title: "Expense of the Month",
                            titleFontColor: "#C0504E",
                            lineColor: "#C0504E",
                            labelFontColor: "#C0504E",
                            tickColor: "#C0504E",
                            titleFontSize: 24,
                            includeZero: false
                        },
                        toolTip: {
                            shared: true
                        },
                        legend: {
                            cursor: "pointer",
                            itemclick: toggleDataSeries
                        },

                        data:

                            [{
                                type: "spline",
                            name: "Income of the Month",
                            showInLegend: true,
                            xValueFormatString: "MMM YYYY",
                            yValueFormatString: "Rs ####",
                            dataPoints: dataPoints

                        },
                        {
                        type: "spline",
                         name: "Expense of the Month",
                           axisYType: "secondary",
                          showInLegend: true,
                           xValueFormatString: "MMM YYYY",
                           yValueFormatString: "Rs ###",
                          dataPoints: dataPointsExpense
                       }
                        ]
                    };
                    $("#chartContainer").CanvasJSChart(chart);

                    function toggleDataSeries(e) {
                        if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
                            e.dataSeries.visible = false;
                        } else {
                            e.dataSeries.visible = true;
                        }
                        e.chart.render();
                    }
                    $.getJSON("/Home/reportup", addData)



        }

    </script>
 <br /><br />

        <button style="margin-left:300px; margin-top:10px" type="button" id="btn" onclick="showchart()">Show my chart</button>
        <div id="chartContainer"  style ="height: 300px; width: 100%;margin: 50px auto;"></div>




 <script type="text/javascript" src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="https://canvasjs.com/assets/script/jquery.canvasjs.min.js"></script>

    <script src="/jquery.canvasjs.min.js"></script>

    <script src="/canvasjs.min.js"></script>
    <script src="/canvasjs.react.js"></script>


</body>



</html>
Muzammil
  • 13
  • 6
  • chart is a json object, you probably meant to do this instead: chart = $("#chartContainer").CanvasJSChart(chart); Also, you should put this in a jsfiddle – tony May 05 '19 at 08:19
  • thanks for your reply @tony but my error is still there – Muzammil May 05 '19 at 08:27
  • Make a jsfiddle and I'll have a look – tony May 05 '19 at 08:30
  • my page name is practice report and it says "PracticeReport:46 Uncaught TypeError: chart.render is not a function at Object.addData [as success] (PracticeReport:46) at j (jquery-1.11.1.min.js:2) at Object.fireWith [as resolveWith] (jquery-1.11.1.min.js:2) at x (jquery-1.11.1.min.js:4) at XMLHttpRequest.b (jquery-1.11.1.min.js:4)" – Muzammil May 05 '19 at 08:31
  • hello did you solve your problem? I'm facing the same problem as well but my data is from ajax. – natsumiyu Oct 29 '19 at 02:55

1 Answers1

0

(1) You never assign the chart to chart

  chart = (something else)
  // reuse the variable, should probably just rename the other one
  chart = $("#chartContainer").CanvasJSChart(chart);

Here's it working

https://jsfiddle.net/mh1zro3v/

(I removed the closure, it wasn't necessary, also I saved it with junk code in, fixed)

tony
  • 2,178
  • 2
  • 23
  • 40
  • thank you for your response but it's still not working for me. – Muzammil May 05 '19 at 08:51
  • Works for me? I see a chart. Bear in mind the data won't load as it's in a jsfiddle – tony May 05 '19 at 08:52
  • While I remember, the other thing I did (which shouldn't affect anything) is remove the react link. You're not using react – tony May 05 '19 at 08:54
  • If you want more help you'll have to be more specific than 'it doesn't work'. Are you saying the jsfiddle doesn't work for you? – tony May 05 '19 at 09:02
  • ii doesn't mean that, i was trying it on localhost. – Muzammil May 05 '19 at 09:04
  • this is what my local host says : "Uncaught TypeError: Cannot read property 'render' of undefined at Object.addData [as success] (PracticeReport:46) at j (jquery-1.11.1.min.js:2) at Object.fireWith [as resolveWith] (jquery-1.11.1.min.js:2) at x (jquery-1.11.1.min.js:4) at XMLHttpRequest.b (jquery-1.11.1.min.js:4)" and it shows error on line that.chart.render(); //here addDataExpense(result) – Muzammil May 05 '19 at 09:08
  • That's because chart isn't defined there. You need to either make chart global or use a closure. You see the code in the jsfiddle that says 'var that = this;' and that.chart, try that. In fact, just copy all the code from the jsfiddle javascript section – tony May 05 '19 at 09:19
  • i just tried it but still my values are not populating on my chart. i tried to add random values in my dataPoints, please have a look [link](https://jsfiddle.net/muzammil25j/pcvu7zqk/12/) – Muzammil May 05 '19 at 10:20
  • when i try to add dummy datasource to expense, line chart will populate but still its not working for my income axis. have a look please [fiddle](https://jsfiddle.net/muzammil25j/pcvu7zqk/12/) – Muzammil May 05 '19 at 10:23
  • (1) You have a cors error, press F12 and it'll tell you (2) You don't need to call render(), the documentation tells you that (3) you are using 'dataPoints' before you define it This is working code https://jsfiddle.net/fgt9Lmo6/ You need to not create the tree until the getJSON call has completed, e.g. move the code in there – tony May 05 '19 at 11:43
  • thanks man..! i wrote an alternative code of it. i couldn't digest this code so i tried another one so i tried another one and in all this way YOUR SUGGESTIONS HELPED ME ALOT :-) – Muzammil May 10 '19 at 16:22