Skip to content Skip to sidebar Skip to footer

Creating 2 Charts Show 2 Different Data In One Graph (asp.net Mvc)

Hello I know it might sound confusing but I hope I can get help .... Well I'm working on asp.net mvc using MYSQL for database , and what I'm trying to make is a 2 charts show 2 d

Solution 1:

You can do it like this:

Place the data for the 2 charts in different ViewBag elements:

public ActionResult BartestChart()
{
   List<string> returnData = getChartData();
   ViewBag.ChartOne = returnData;
   List<string> returnData1 = getChartData2();
   ViewBag.ChartTwo = returnData1;
   return View();
}

And in your View:

@{
    ViewBag.Title = "BartestChart";
    Layout = "~/Views/Shared/_MainLayout.cshtml";
    int x = 0;
}

<!DOCTYPE html><htmlxmlns="http://www.w3.org/1999/xhtml"><bodystyle="height: 331px"><divstyle="padding-top:50px"></div><div><canvasid="Chart"width="500"height="500"style="border:dashed;border-color:lightgray"></canvas></div><scriptsrc="../Scripts/jquery-1.10.2.min.js"></script><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scriptsrc="../Scripts/Chart.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js"></script><script>var chartLabel;
        var chartData = [];
    </script>

    @foreach (var label in (List<string>)ViewBag.ChartOne)
    {
        if (x == 1)
        {
            <script>
                        chartData = @label;
                        console.log(chartData);
            </script>
        }
        else
        {
            <script>
                        chartLabel = "@label";
                        chartLabel = chartLabel.replace(/g/g, "\"");
                        chartLabel = JSON.parse(chartLabel);
                        console.log(chartLabel);
            </script>
        }
        x++;

    }
    <script>var chartLabel1;
        var chartData1 = [];
    </script>


@foreach (var label in (List<string>)ViewBag.ChartTwo)
{
    <script>
                    chartData1 = @label;
                    console.log(chartData1);
    </script>

}
<scripttype="text/javascript">var ctx = document.getElementById("Chart").getContext('2d');

                var barData = {
                    labels: chartLabel,
                    datasets: [
                                {
                                    label: 'Efficiency',
                                    fillColor: "IndianRed",
                                    strokeColor: "IndianRed",
                                    data: chartData1

                                } ,

                                {
                                    label: 'Efficiency',
                                    fillColor: "Black",
                                    strokeColor: "Black",
                                    data: chartData

                                } ]
    };

                var skillsChart = newChart(ctx).Bar(barData);

</script></body></html>

Solution 2:

You can use Third-Party charts Like Google charts or AM Charts Link For AM Charts Website

Solution 3:

Here is the screenshot

The view :

@{
    ViewBag.Title = "BartestChart";
    Layout = "~/Views/Shared/_MainLayout.cshtml";
    int x = 0;
}

<!DOCTYPE html><htmlxmlns="http://www.w3.org/1999/xhtml"><bodystyle="height: 331px"><divstyle="padding-top:50px"></div><div><canvasid="Chart"width="500"height="500"style="border:dashed;border-color:lightgray"></canvas></div><scriptsrc="../Scripts/jquery-1.10.2.min.js"></script><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scriptsrc="../Scripts/Chart.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js"></script><script>var chartLabel;
        var chartData = [];
    </script>

    @foreach (var label in (List<string>)ViewBag.ChartOne)
    {
        if (x == 1)
        {
            <script>
                        chartData = @label;
                        console.log(chartData);
            </script>
        }
        else
        {
            <script>
                        chartLabel = "@label";
                        chartLabel = chartLabel.replace(/g/g, "\"");
                        chartLabel = JSON.parse(chartLabel);
                        console.log(chartLabel);
            </script>
        }
        x++;

    }
    <script>var chartLabel1;
        var chartData1 = [];
    </script>

    @foreach (var label in (List<string>)ViewBag.ChartTwo)
    {
        if (x == 1)
        {
            <script>
                        chartData1 = @label;
                        console.log(chartData1);
            </script>
        }
        else
        {
            <script>
                        chartLabel1 = "@label";
                        chartLabel1 = chartLabel1.replace(/g/g, "\"");
                        chartLabel1 = JSON.parse(chartLabel1);
                        console.log(chartLabel1);
            </script>
        }
        x++;

    }
    <scripttype="text/javascript">var ctx = document.getElementById("Chart").getContext('2d');

                    var barData = {
                        labels: chartLabel,
                        datasets: [
                                    {
                                        label: 'Efficiency',
                                        fillColor: "cadetblue",
                                        strokeColor: "cadetblue",
                                        data: chartData

                                    } ,

                                    {
                                        label: 'Efficiency',
                                        fillColor: "green",
                                        strokeColor: "green",
                                        data: chartData1

                                    } ]
        };

                    var skillsChart = newChart(ctx).Bar(barData);

    </script></body></html>

The change in the Controller:

public ActionResult BartestChart()
        {
            List<string> returnData = getChartData();
            ViewBag.ChartOne = returnData;
            List<string> returnData1 = getChartData2();
            ViewBag.ChartTwo = returnData1;
            return View();
        }

Post a Comment for "Creating 2 Charts Show 2 Different Data In One Graph (asp.net Mvc)"