Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi i have a javascript code and i want to add it to my page in Default.aspx.cs . I tried this
C#
string sb = "";


        sb+=(@" <script type='text/javascript'>");
        sb+=(@"var chart;");
        sb+=(@" var chartData = [");
        sb+=(@"{");
        sb+=(@" 'year': 2005,");
        sb+=(@"  'income': 23.5,");
        sb+=(@"'expenses': 18.1},");
        sb+=(@"AmCharts.ready(function () {");
        sb+=(@"chart = new AmCharts.AmSerialChart(); chart.dataProvider = chartData;                chart.categoryField = 'year'; chart.startDuration = 1; chart.plotAreaBorderColor = '#DADADA'; chart.plotAreaBorderAlpha = 1; // this single line makes the chart a bar chart chart.rotate = true;");
        sb+=(@"var categoryAxis = chart.categoryAxis;                categoryAxis.gridPosition = 'start';                categoryAxis.gridAlpha = 0.1;                categoryAxis.axisAlpha = 0;");
        sb+=(@"                var valueAxis = new AmCharts.ValueAxis();
            valueAxis.axisAlpha = 0;
            valueAxis.gridAlpha = 0.1;
            valueAxis.position = 'top';
            chart.addValueAxis(valueAxis);");
        sb+=(@"var graph1 = new AmCharts.AmGraph();                graph1.type = 'column';                graph1.title = 'Income';                graph1.valueField = 'income';                graph1.balloonText = 'Income:[[value]]';                graph1.lineAlpha = 0;                graph1.fillColors = '#ADD981';graph1.fillAlphas = 1;               chart.addGraph(graph1);");
        sb+=(@"                var graph2 = new AmCharts.AmGraph();                graph2.type = 'column';                graph2.title = 'Expenses';                graph2.valueField = 'expenses';                graph2.balloonText = 'Expenses:[[value]]';                graph2.lineAlpha = 0;                graph2.fillColors = '#81acd9';                graph2.fillAlphas = 1;      chart.addGraph(graph2);");
        sb+=(@"var legend = new AmCharts.AmLegend();                chart.addLegend(legend);                chart.creditsPosition = 'top-right';                // WRITE                chart.write('chartdiv');            });  ");
        sb+=(@"</script>");


        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "MyFun1", sb, false);


the code works when directly add on aspx page but when i try this and tried any other codes but i can't run it on my page. Thanks for your interest
Posted
Comments
Thanks7872 27-Jun-14 10:41am    
Why to add like this? Why not to simply call javascript function from here and move all this code to that function?
enoua 27-Jun-14 10:43am    
bcs i will change values dynamically from database
Thanks7872 27-Jun-14 12:01pm    
so what? Send those values as arguments to javascript function.

You have two things going wrong here.

Problem #1:
The JavaScript code is missing some pieces. You declare a variable called chartData, set it equal to an array, where the first element is an object ({'year': 2005, ...etc. }), and the second element is the return value of a function called AmCharts.ready() when called with a big long function parameter.

Except you have a semicolon at the end of the AmCharts.ready() call, and no close bracket on the array, so your code looks like this:

JavaScript
...
var chartData =
  [
    {
      'year': 2005,
      ...
    },
    AmCharts.ready(function() {...});

It just ends with a semicolon that shouldn't be there, and no "];" to finish off the declaration of the array. If your code works when pasted in, then it probably is an error in transcribing it into the form you have it here (building a string gradually).

Problem #2:
I think you're commenting out some of your code by accident. See the two spots where you introduce comments ("// this single line..." and "// WRITE")? In JavaScript, the "//" comment indicator marks the entire rest of the line as a comment. The way your code is written to assemble the pieces, you have a bunch of lines of code that appear to be ending up, in the finished string, on the same line as the comment, because there's no line break between the comment and the next actual line of code.
 
Share this answer
 
Comments
Trajan McGill 27-Jun-14 11:36am    
As the other solution points out, it is also possible that you meant to have the "];" *before* the AmCharts.ready() call, which is actually probably more likely, but I don't have any idea what AmCharts.ready() does or whether you mean to stick the results in the array you're creating or to call it after creating the array.
the code that is generated by your concatenation logic is like that

XML
<script type='text/javascript'>var chart; var chartData = [{ 'year': 2005,  'income': 23.5,'expenses': 18.1},AmCharts.ready(function () {chart = new AmCharts.AmSerialChart(); chart.dataProvider = chartData;                chart.categoryField = 'year'; chart.startDuration = 1; chart.plotAreaBorderColor = '#DADADA'; chart.plotAreaBorderAlpha = 1; // this single line makes the chart a bar chart chart.rotate = true;var categoryAxis = chart.categoryAxis;                categoryAxis.gridPosition = 'start';                categoryAxis.gridAlpha = 0.1;                categoryAxis.axisAlpha = 0;                var valueAxis = new AmCharts.ValueAxis();
           valueAxis.axisAlpha = 0;
           valueAxis.gridAlpha = 0.1;
           valueAxis.position = 'top';
           chart.addValueAxis(valueAxis);var graph1 = new AmCharts.AmGraph();                graph1.type = 'column';                graph1.title = 'Income';                graph1.valueField = 'income';                graph1.balloonText = 'Income:[[value]]';                graph1.lineAlpha = 0;                graph1.fillColors = '#ADD981';graph1.fillAlphas = 1;               chart.addGraph(graph1);                var graph2 = new AmCharts.AmGraph();                graph2.type = 'column';                graph2.title = 'Expenses';                graph2.valueField = 'expenses';                graph2.balloonText = 'Expenses:[[value]]';                graph2.lineAlpha = 0;                graph2.fillColors = '#81acd9';                graph2.fillAlphas = 1;      chart.addGraph(graph2);var legend = new AmCharts.AmLegend();                chart.addLegend(legend);                chart.creditsPosition = 'top-right';                // WRITE                chart.write('chartdiv');            });  </script>


as you see some part of your code is commented out, first need to fix that part and the thing is
chartData = [
starts with '[' but there is no ']'

also instead of
,AmCharts.ready
it must be
;AmCharts.ready

so basically you should change your code to the following one :

C#
protected void Page_Load(object sender, EventArgs e)
    {
        string sb = "";


        sb += (@" <script type='text/javascript'>");
        sb += (@"var chart;");
        sb += (@" var chartData = [");
        sb += (@"{");
        sb += (@" 'year': 2005,");
        sb += (@"  'income': 23.5,");
        sb += (@"'expenses': 18.1}];");
        sb += (@"AmCharts.ready(function () {");
        sb += (@"chart = new AmCharts.AmSerialChart(); chart.dataProvider = chartData;                chart.categoryField = 'year'; chart.startDuration = 1; chart.plotAreaBorderColor = '#DADADA'; chart.plotAreaBorderAlpha = 1;  chart.rotate = true;");
        sb += (@"var categoryAxis = chart.categoryAxis;                categoryAxis.gridPosition = 'start';                categoryAxis.gridAlpha = 0.1;                categoryAxis.axisAlpha = 0;");
        sb += (@"                var valueAxis = new AmCharts.ValueAxis();
            valueAxis.axisAlpha = 0;
            valueAxis.gridAlpha = 0.1;
            valueAxis.position = 'top';
            chart.addValueAxis(valueAxis);");
        sb += (@"var graph1 = new AmCharts.AmGraph();                graph1.type = 'column';                graph1.title = 'Income';                graph1.valueField = 'income';                graph1.balloonText = 'Income:[[value]]';                graph1.lineAlpha = 0;                graph1.fillColors = '#ADD981';graph1.fillAlphas = 1;               chart.addGraph(graph1);");
        sb += (@"                var graph2 = new AmCharts.AmGraph();                graph2.type = 'column';                graph2.title = 'Expenses';                graph2.valueField = 'expenses';                graph2.balloonText = 'Expenses:[[value]]';                graph2.lineAlpha = 0;                graph2.fillColors = '#81acd9';                graph2.fillAlphas = 1;      chart.addGraph(graph2);");
        sb += (@"var legend = new AmCharts.AmLegend();                chart.addLegend(legend);                chart.creditsPosition = 'top-right';                         chart.write('chartdiv');            });  ");
        sb += (@"</script>");


        ScriptManager.RegisterStartupScript(Page, GetType(), "MyFun1", sb, false);
    }


there is an example here for more information

CSS
<script type="text/javascript">
            var chart;

            var chartData = [{
                "country": "USA",
                    "visits": 4025,
                    "color": "#FF0F00"
            }, {
                "country": "China",
                    "visits": 1882,
                    "color": "#FF6600"
            }, {
                "country": "Japan",
                    "visits": 1809,
                    "color": "#FF9E01"
            }, {
                "country": "Germany",
                    "visits": 1322,
                    "color": "#FCD202"
            }, {
                "country": "UK",
                    "visits": 1122,
                    "color": "#F8FF01"
            }, {
                "country": "France",
                    "visits": 1114,
                    "color": "#B0DE09"
            }, {
                "country": "India",
                    "visits": 984,
                    "color": "#04D215"
            }, {
                "country": "Spain",
                    "visits": 711,
                    "color": "#0D8ECF"
            }, {
                "country": "Netherlands",
                    "visits": 665,
                    "color": "#0D52D1"
            }, {
                "country": "Russia",
                    "visits": 580,
                    "color": "#2A0CD0"
            }, {
                "country": "South Korea",
                    "visits": 443,
                    "color": "#8A0CCF"
            }, {
                "country": "Canada",
                    "visits": 441,
                    "color": "#CD0D74"
            }, {
                "country": "Brazil",
                    "visits": 395,
                    "color": "#754DEB"
            }, {
                "country": "Italy",
                    "visits": 386,
                    "color": "#DDDDDD"
            }, {
                "country": "Australia",
                    "visits": 384,
                    "color": "#999999"
            }, {
                "country": "Taiwan",
                    "visits": 338,
                    "color": "#333333"
            }, {
                "country": "Poland",
                    "visits": 328,
                    "color": "#000000"
            }];


            var chart = AmCharts.makeChart("chartdiv", {
                type: "serial",
                dataProvider: chartData,
                categoryField: "country",
                pathToImages:"../amcharts/images/",
                depth3D: 20,
                angle: 30,

                categoryAxis: {
                    labelRotation: 90,
                    gridPosition: "start"
                },

                valueAxes: [{
                    title: "Visitors"
                }],

                graphs: [{

                    valueField: "visits",
                    colorField: "color",
                    type: "column",
                    lineAlpha: 0,
                    fillAlphas: 1
                }],

                chartCursor: {
                    cursorAlpha: 0,
                    zoomable: false,
                    categoryBalloonEnabled: false
                },

                amExport: {
                    top: 21,
                    right: 21,
                    buttonColor: '#EFEFEF',
                    buttonRollOverColor:'#DDDDDD',
                    exportPNG:true,
                    exportJPG:true,
                    exportPDF:true,
                    exportSVG:true
                }
            });
        </script>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900