Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Combo Charts?

enter image description here

I have 4 entities and I show them for 4 days. But first and last days I cant see other 2 entities.In 3 August I cant see T0,T1. In 6 August I cant see T2,T3.

Codes

var evalledData = eval("(" + result.chartData + ")");
var ac = new google.visualization.ComboChart($("#chart_div_ay").get(0));

ac.draw(new google.visualization.DataTable(evalledData, 0.5), {
     //title: 'Son 7 günlük sayaç okumalarının toplamı.',
     width: '100%',
     height: 300,
     vAxis: { title: "kW" },
     hAxis: { title: "Gün" },
     seriesType: "bars",
     series: { 5: { type: "line"} }
});

Controller:

public ActionResult MusteriSayaclariOkumalariChartDataTable(DateTime startDate, DateTime endDate, int? musteri_id)
    {
        IEnumerable<TblSayacOkumalari> sayac_okumalari = entity.TblSayacOkumalari;
        var sonuc = from s in sayac_okumalari
                    where s.TblSayaclar.musteri_id == musteri_id && s.okuma_tarihi.Value >= startDate && s.okuma_tarihi.Value <= endDate
                    group s by new { date = new DateTime(((DateTime)s.okuma_tarihi).Year, ((DateTime)s.okuma_tarihi).Month, ((DateTime)s.okuma_tarihi).Day) } into g
                    select new
                    {
                        okuma_tarihi = g.Key,
                        T1 = g.Sum(x => x.kullanim_T1) / 1000,
                        T2 = g.Sum(x => x.kullanim_T2) / 1000,
                        T3 = g.Sum(x => x.kullanim_T3) / 1000,
                        T4 = g.Sum(x => x.kullanim_T0) / 1000
                    };

        //Get your data table from DB or other source
        DataTable chartTable = new DataTable();

        chartTable.Columns.Add("Tarih").DataType = System.Type.GetType("System.DateTime");
        chartTable.Columns.Add("T1").DataType = System.Type.GetType("System.Double");
        chartTable.Columns.Add("T2").DataType = System.Type.GetType("System.Double");
        chartTable.Columns.Add("T3").DataType = System.Type.GetType("System.Double");
        chartTable.Columns.Add("Toplam").DataType = System.Type.GetType("System.Double");
        foreach (var item in sonuc)
        {
            chartTable.Rows.Add(item.okuma_tarihi.date, item.T1.Value, item.T2.Value, item.T3.Value, item.T4.Value);
        }

        //convert datetime value to google datetype, if your first column is date
        Bortosky
            .Google
            .Visualization
            .GoogleDataTable
            .SetGoogleDateType(chartTable.Columns["Tarih"],
                 Bortosky.Google.Visualization.GoogleDateType.Date);
        //convert DataTable to GoogleDataTable
        var googleDataTable =
                    new Bortosky.Google.Visualization.GoogleDataTable(chartTable);
        //Pass the google datatable to UI as json string

        return new JsonResult
        {
            Data = new
            {
                success = true,
                chartData = googleDataTable.GetJson()
            },
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    }

This action return json as google examples custom data.

evalledData output:

enter image description here

Is there any option about this problem?

Thanks.

like image 884
AliRıza Adıyahşi Avatar asked Aug 06 '12 12:08

AliRıza Adıyahşi


People also ask

Can you do combo charts in Google Sheets?

Yes, you can make a digital chart that shows more than one data type in different ways. Google Sheets lets you make a combo chart in an easy and cloud-shareable manner.

What is a combo chart?

A combo chart is a combination of two column charts, two line graphs, or a column chart and a line graph. You can make a combo chart with a single dataset or with two datasets that share a common string field. Combo charts can answer questions about your data, such as: What are the trends for the same categories?

How do you create a combo chart?

Click anywhere in the chart you want to change to a combo chart to show the CHART TOOLS. Click DESIGN > Change Chart Type. On the All Charts tab, choose Combo, and then pick the Clustered Column - Line on Secondary Axis chart.


1 Answers

I recently had to build a chart like this. Please consider my code for your solution:

Put this in your Controller:

<EmployeeAuthorize()>
Function WeightAreaChartData() As JsonResult

    Dim myData = db.Tbl_Weights.Where(Function(x) x.Weight_Employee_ID).OrderBy(Function(x) x.Weight_Create_Date)

    Dim data = New List(Of Object)

    data.Add(New Object() {"Date", "Your Weight"})

    For Each i As Tbl_Weight In myData

        data.Add(New Object() {DateTime.Parse(i.Weight_Create_Date).Day, i.Weight_Amount})

    Next

    Return Json(data, JsonRequestBehavior.AllowGet)

End Function

Put this in your view; changing the $.post() URL accordingly:

<script type="text/javascript">
    google.load("visualization", "1", { packages: ["corechart"] });
    google.setOnLoadCallback(drawChart);

    function drawChart() {
        $.post('/Weight/WeightAreaChartData', {},
            function (data) {
                var tdata = new google.visualization.arrayToDataTable(data);

                var options = {
                    title: 'Weight Lost & Gained This Month',
                    hAxis: { title: 'Day of Month', titleTextStyle: { color: '#1E4A08'} },
                    vAxis: { title: 'Lbs.', titleTextStyle: { color: '#1E4A08'} },
                    chartArea: { left: 50, top: 30, width: "75%" },
                    colors: ['#FF8100']
                };

                var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
                chart.draw(tdata, options);
            });
    }
</script>

<div id="chart_div" style="width: 580px; height: 200px;"></div>

To fix your specific issue of the bars being cut off, I believe you can use this in your options:

hAxis: {
  viewWindowMode: 'pretty'
}

Like this:

var options = {
                        title: 'Weight Lost & Gained This Month',
                        hAxis: { title: 'Day of Month', titleTextStyle: { color: '#1E4A08'} },
                        vAxis: { title: 'Lbs.', titleTextStyle: { color: '#1E4A08' } },
                        chartArea: { left: 50, top: 30, width: "75%" },
                        colors: ['#FF8100', 'blue'],
                       hAxis: {

                            viewWindowMode: 'pretty'
                          }
                    };
like image 169
user1477388 Avatar answered Oct 07 '22 18:10

user1477388