Ørjan
Ørjan

Reputation: 2879

Highcharts won't render in my MVC4 .NET project

I'm trying to get Highcharts to work in my project. When running the code in Notepad++ it works, but when inserting the same code into my .NET MVC4 project it wont render anything.

Heres the code:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script type="text/javascript">
    $(function () {
        $('#container').highcharts({
            chart: {
                plotBackgroundColor: null,
                plotBorderWidth: 1,//null,
                plotShadow: false
            },
            title: {
                text: 'Browser market shares at a specific website, 2014'
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                        style: {
                            color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                        }
                    }
                }
            },
            series: [{
                type: 'pie',
                name: 'Browser share',
                data: [
                    ['Firefox', 45.0],
                    ['IE', 26.8],
                    {
                        name: 'Chrome',
                        y: 12.8,
                        sliced: true,
                        selected: true
                    },
                    ['Safari', 8.5],
                    ['Opera', 6.2],
                    ['Others', 0.7]
                ]
            }]
        });
    });
</script>

The following error message is returned from chrome:

Picture of the error message

While Firefox says:

TypeError: $(...).highcharts is not a function

Anyone got any idea why it won't render?

Upvotes: 1

Views: 390

Answers (1)

&#216;rjan
&#216;rjan

Reputation: 2879

I found the solution here: Uncaught TypeError: undefined is not a function - Highcharts - MVC

I changed the following code:

 $(function () {
        $('#container').highcharts({
            title: {
                text: 'Monthly Average Temperature',
                x: -20 //center
         },
         // rest of code omitted 

To the following:

$(function () {
    Chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container'
        },

        title: {
             text: 'Monthly Average Temperature',
             x: -20 //center
         },
         //rest of code omitted

And now it works fine :)

Upvotes: 1

Related Questions