Using data from model in Chart.js MVC c#












1















I am trying to make a Chart with Chart.js but i cant get the data from my model into the chart... can someone help me with this?



<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>

<canvas id="myChart" width="400" height="400"></canvas>
<script >
var ctx = document.getElementById('myChart').getContext('2d');
@{
List<String> listKeys = new List<string>();
List<int> listValues = new List<int>();
foreach(var x in Model.PageViews)
{
listKeys.Add(x.Key + "");
listValues.Add(x.Value);
}
}

var chart = new Chart(ctx, {
type: 'line',
data: {
labels: @listKeys,
datasets: [{
label: "My First dataset",
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: @listValues
}]
},
options: {}
});



I've tried multiple things but nothing seems to work...










share|improve this question




















  • 1





    Your model should contain properties IEnumerable<string> Labels and IEnumerable<int> Values and then you use var labels = @Html.Raw.Json.Encode(Model.Labels)) ect to convert the model to a javascript array

    – user3559349
    Nov 20 '18 at 9:52
















1















I am trying to make a Chart with Chart.js but i cant get the data from my model into the chart... can someone help me with this?



<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>

<canvas id="myChart" width="400" height="400"></canvas>
<script >
var ctx = document.getElementById('myChart').getContext('2d');
@{
List<String> listKeys = new List<string>();
List<int> listValues = new List<int>();
foreach(var x in Model.PageViews)
{
listKeys.Add(x.Key + "");
listValues.Add(x.Value);
}
}

var chart = new Chart(ctx, {
type: 'line',
data: {
labels: @listKeys,
datasets: [{
label: "My First dataset",
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: @listValues
}]
},
options: {}
});



I've tried multiple things but nothing seems to work...










share|improve this question




















  • 1





    Your model should contain properties IEnumerable<string> Labels and IEnumerable<int> Values and then you use var labels = @Html.Raw.Json.Encode(Model.Labels)) ect to convert the model to a javascript array

    – user3559349
    Nov 20 '18 at 9:52














1












1








1








I am trying to make a Chart with Chart.js but i cant get the data from my model into the chart... can someone help me with this?



<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>

<canvas id="myChart" width="400" height="400"></canvas>
<script >
var ctx = document.getElementById('myChart').getContext('2d');
@{
List<String> listKeys = new List<string>();
List<int> listValues = new List<int>();
foreach(var x in Model.PageViews)
{
listKeys.Add(x.Key + "");
listValues.Add(x.Value);
}
}

var chart = new Chart(ctx, {
type: 'line',
data: {
labels: @listKeys,
datasets: [{
label: "My First dataset",
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: @listValues
}]
},
options: {}
});



I've tried multiple things but nothing seems to work...










share|improve this question
















I am trying to make a Chart with Chart.js but i cant get the data from my model into the chart... can someone help me with this?



<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>

<canvas id="myChart" width="400" height="400"></canvas>
<script >
var ctx = document.getElementById('myChart').getContext('2d');
@{
List<String> listKeys = new List<string>();
List<int> listValues = new List<int>();
foreach(var x in Model.PageViews)
{
listKeys.Add(x.Key + "");
listValues.Add(x.Value);
}
}

var chart = new Chart(ctx, {
type: 'line',
data: {
labels: @listKeys,
datasets: [{
label: "My First dataset",
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: @listValues
}]
},
options: {}
});



I've tried multiple things but nothing seems to work...







javascript c# asp.net-mvc model chart.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 9:52







Aram Mutlu

















asked Nov 20 '18 at 9:46









Aram MutluAram Mutlu

277




277








  • 1





    Your model should contain properties IEnumerable<string> Labels and IEnumerable<int> Values and then you use var labels = @Html.Raw.Json.Encode(Model.Labels)) ect to convert the model to a javascript array

    – user3559349
    Nov 20 '18 at 9:52














  • 1





    Your model should contain properties IEnumerable<string> Labels and IEnumerable<int> Values and then you use var labels = @Html.Raw.Json.Encode(Model.Labels)) ect to convert the model to a javascript array

    – user3559349
    Nov 20 '18 at 9:52








1




1





Your model should contain properties IEnumerable<string> Labels and IEnumerable<int> Values and then you use var labels = @Html.Raw.Json.Encode(Model.Labels)) ect to convert the model to a javascript array

– user3559349
Nov 20 '18 at 9:52





Your model should contain properties IEnumerable<string> Labels and IEnumerable<int> Values and then you use var labels = @Html.Raw.Json.Encode(Model.Labels)) ect to convert the model to a javascript array

– user3559349
Nov 20 '18 at 9:52












2 Answers
2






active

oldest

votes


















1














The code inside block @{ } is server side.
Other content of the script-tag is client side.



C# runs ToString() method when you use @listKeys/@listValues. It generates string like System.Collections.Generic.List[System.String] instead of content of the lists. You need to generate json objects instead.



Use @Html.Raw(Json.Serialize(listKeys)) instead of @listKeys to get correct json-object with data.



P.S. It's not good practice to use a lot of server-logic inside views. You can remove your @{} block and get json for Model.PageViews.Keys and Model.PageViews.Values






share|improve this answer
























  • Thankyou Sergey, this helped me!

    – Aram Mutlu
    Nov 21 '18 at 12:08



















0














It's not good practice to use .NET variables in JavaScript code.
The right way is to create hidden html elements for values who you want to use in js function or use API that returns chart data as Json object.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390193%2fusing-data-from-model-in-chart-js-mvc-c-sharp%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The code inside block @{ } is server side.
    Other content of the script-tag is client side.



    C# runs ToString() method when you use @listKeys/@listValues. It generates string like System.Collections.Generic.List[System.String] instead of content of the lists. You need to generate json objects instead.



    Use @Html.Raw(Json.Serialize(listKeys)) instead of @listKeys to get correct json-object with data.



    P.S. It's not good practice to use a lot of server-logic inside views. You can remove your @{} block and get json for Model.PageViews.Keys and Model.PageViews.Values






    share|improve this answer
























    • Thankyou Sergey, this helped me!

      – Aram Mutlu
      Nov 21 '18 at 12:08
















    1














    The code inside block @{ } is server side.
    Other content of the script-tag is client side.



    C# runs ToString() method when you use @listKeys/@listValues. It generates string like System.Collections.Generic.List[System.String] instead of content of the lists. You need to generate json objects instead.



    Use @Html.Raw(Json.Serialize(listKeys)) instead of @listKeys to get correct json-object with data.



    P.S. It's not good practice to use a lot of server-logic inside views. You can remove your @{} block and get json for Model.PageViews.Keys and Model.PageViews.Values






    share|improve this answer
























    • Thankyou Sergey, this helped me!

      – Aram Mutlu
      Nov 21 '18 at 12:08














    1












    1








    1







    The code inside block @{ } is server side.
    Other content of the script-tag is client side.



    C# runs ToString() method when you use @listKeys/@listValues. It generates string like System.Collections.Generic.List[System.String] instead of content of the lists. You need to generate json objects instead.



    Use @Html.Raw(Json.Serialize(listKeys)) instead of @listKeys to get correct json-object with data.



    P.S. It's not good practice to use a lot of server-logic inside views. You can remove your @{} block and get json for Model.PageViews.Keys and Model.PageViews.Values






    share|improve this answer













    The code inside block @{ } is server side.
    Other content of the script-tag is client side.



    C# runs ToString() method when you use @listKeys/@listValues. It generates string like System.Collections.Generic.List[System.String] instead of content of the lists. You need to generate json objects instead.



    Use @Html.Raw(Json.Serialize(listKeys)) instead of @listKeys to get correct json-object with data.



    P.S. It's not good practice to use a lot of server-logic inside views. You can remove your @{} block and get json for Model.PageViews.Keys and Model.PageViews.Values







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 12:12









    Sergey SviridovSergey Sviridov

    8613




    8613













    • Thankyou Sergey, this helped me!

      – Aram Mutlu
      Nov 21 '18 at 12:08



















    • Thankyou Sergey, this helped me!

      – Aram Mutlu
      Nov 21 '18 at 12:08

















    Thankyou Sergey, this helped me!

    – Aram Mutlu
    Nov 21 '18 at 12:08





    Thankyou Sergey, this helped me!

    – Aram Mutlu
    Nov 21 '18 at 12:08













    0














    It's not good practice to use .NET variables in JavaScript code.
    The right way is to create hidden html elements for values who you want to use in js function or use API that returns chart data as Json object.






    share|improve this answer






























      0














      It's not good practice to use .NET variables in JavaScript code.
      The right way is to create hidden html elements for values who you want to use in js function or use API that returns chart data as Json object.






      share|improve this answer




























        0












        0








        0







        It's not good practice to use .NET variables in JavaScript code.
        The right way is to create hidden html elements for values who you want to use in js function or use API that returns chart data as Json object.






        share|improve this answer















        It's not good practice to use .NET variables in JavaScript code.
        The right way is to create hidden html elements for values who you want to use in js function or use API that returns chart data as Json object.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 14:04

























        answered Nov 20 '18 at 11:10









        Aleksandar AleksandrovAleksandar Aleksandrov

        64




        64






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390193%2fusing-data-from-model-in-chart-js-mvc-c-sharp%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)