locked
Pass dynamic values to x and y axis in char RRS feed

  • Question

  • User1624753842 posted

    I generating chart using web.helpers library

    my question is how i pass x and y axis values dynamically

    in my array contain 3 values . how i  pass this x axis to chart

    string[] fruits = new string[] { "Apple", "Orange", "Grape" };

     new System.Web.Helpers.Chart(width: 500, height: 300, theme: ChartTheme.Blue)
                  .AddTitle("Chart for Fruits")

                  
                     .AddSeries(
                          chartType: "pie",
                           xValue: new[] { },// here i need to pass fruits array value?
                           

                           
                         yValues: new[] { "10", "25", "50" })
                      

                    .ToWebImage().GetBytes());

    Wednesday, November 15, 2017 9:15 AM

All replies

  • User2103319870 posted

    in my array contain 3 values . how i  pass this x axis to chart

    You can try with the below apporach

    Create  a model class to hold the data 

     public class SampleChartData
        {
            public string[] XAxisValues { get; set; }
    
            public string[] YAxisValues { get; set; }
        }

    Change the name as per your need

    Now in controller Populate the Above model with data

     public class SampleChartController : Controller
        {
            // GET: SampleChart
            public ActionResult Index()
            {
    
                //Populate Data
                //Here You can get data from Database and create a String array
                SampleChartData chartData = new SampleChartData();
                chartData.XAxisValues = new[] { "Apple", "Orange", "Grape" };
                chartData.YAxisValues = new[] { "10", "25", "50" };
    
    
                return View(chartData);
            }
        }

    we can pass data from Model in View like below

    @model MVCWebApplicationDemo.Models.SampleChartData
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div>
            @{
                var myChart = new Chart(width: 600, height: 400, theme: ChartTheme.Blue)
                    .AddTitle("Chart for Fruits")
                    .AddSeries(
                        chartType: "pie",
                        xValue: @Model.XAxisValues,
                        yValues: @Model.YAxisValues)
                        .ToWebImage().GetBytes());
            }
        </div>
    </body>
    </html>
    

    SOURCE URL

    Wednesday, November 15, 2017 9:32 PM
  • User-832373396 posted

    Hi Binumon,

         chartType: "pie",
                           xValue: new[] { },// here i need to pass fruits array value?

    After tested, it works well:

      @{
                string[] fruits = new string[] { "Apple", "Orange", "Grape" };
                var myChart = new Chart(width: 600, height: 400, theme: ChartTheme.Blue)
                    .AddTitle("Chart for Fruits")
                    .AddSeries(
                        chartType: "pie",
                        xValue: fruits ,// pass fruits directly
                        yValues: new[] { "10", "25", "50" }).Write();
    
            }

    Please let me know if any question, welcome to back!

    With regards, Angelia Jolie

    Friday, November 17, 2017 10:20 AM