Lo primero que necesitamos es nuestra clase "GoogleCharts" (antes Charts) personalizada:
using System.Collections.Generic;
public class GoogleCharts
{
public List<ChartColumn> cols = new List<ChartColumn>();
public List<ChartRow> rows = new List<ChartRow>();
public struct ChartColumn
{
public string id;
public string label;
public string pattern;
public string type;
public string role;
}
public struct ChartRow
{
public List<ChartRowValue> c;
}
public struct ChartRowValue
{
public object v;
public object f;
}
public void AddColumn(ChartColumn col) { cols.Add(col); }
public void AddRow(ChartRow row) { rows.Add(row); }
}
Agregamos una página C# ASP.Net llamada "barras-multiples.aspx", con el siguiente código:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TyroDeveloper</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
//Lineas
//Pastel
$.ajax({
type: 'POST',
url: "barras-multiples.aspx/GraficaBarras",
dataType: "json",
contentType: 'application/json',
async: false,
success: function (result) {
var data = new google.visualization.DataTable(result.d);
var options = {
title: '',
hAxis: {
title: 'Meses del Año'
},
vAxis: {
title: 'Total'
},
backgroundColor: { fill: 'transparent' },
'height': 300,
/*Los colores deben coincidir*/
colors: ['#FF0000', '#00CC00','#9900CC']
};
target = document.getElementById('bar-chart');
var chart = new google.visualization.ColumnChart(target);
chart.draw(data, options);
}
});
}
</script>
<style>
body {
font-family: Verdana;
}
</style>
</head>
<body>
<form id="frmChart" runat="server">
<div>
<div id="bar-chart"></div>
</div>
</form>
</body>
</html>
Con el siguiente código C#:
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Web.Services;
public partial class googlecharts_barras_multiples : System.Web.UI.Page
{
[WebMethod]
public static string GraficaBarras()
{
GoogleCharts chart = new GoogleCharts();
//Agregar columnas
chart.cols.Add(new GoogleCharts.ChartColumn()
{
label = "Mes",
type = "string"
});
chart.cols.Add(new GoogleCharts.ChartColumn()
{
label = "Fresas",
type = "number"
});
chart.cols.Add(new GoogleCharts.ChartColumn()
{
role = "style",
type = "string"
});
chart.cols.Add(new GoogleCharts.ChartColumn()
{
label = "Manzanas",
type = "number"
});
chart.cols.Add(new GoogleCharts.ChartColumn()
{
role = "style",
type = "string"
});
chart.cols.Add(new GoogleCharts.ChartColumn()
{
label = "Uvas",
type = "number"
});
chart.cols.Add(new GoogleCharts.ChartColumn()
{
role = "style",
type = "string"
});
//Agregar datos
//Agregar Enero
chart.rows.Add(new GoogleCharts.ChartRow()
{
c = new List() {
{ new GoogleCharts.ChartRowValue() { v = "Enero" } },
{ new GoogleCharts.ChartRowValue() { v = 20 } },
{ new GoogleCharts.ChartRowValue() { v = "color: #FF0000" } },
{ new GoogleCharts.ChartRowValue() { v = 5 } },
{ new GoogleCharts.ChartRowValue() { v = "color: #00CC00" } },
{ new GoogleCharts.ChartRowValue() { v = 13 } },
{ new GoogleCharts.ChartRowValue() { v = "color: #9900CC" } }
}
});
//Agregar Febrero
chart.rows.Add(new GoogleCharts.ChartRow()
{
c = new List() {
{ new GoogleCharts.ChartRowValue() { v = "Febrero" } },
{ new GoogleCharts.ChartRowValue() { v = 12 } },
{ new GoogleCharts.ChartRowValue() { v = "color: #FF0000" } },
{ new GoogleCharts.ChartRowValue() { v = 21 } },
{ new GoogleCharts.ChartRowValue() { v = "color: #00CC00" } },
{ new GoogleCharts.ChartRowValue() { v = 12 } },
{ new GoogleCharts.ChartRowValue() { v = "color: #9900CC" } }
}
});
//Convertir clase a Json
return new JavaScriptSerializer().Serialize(chart);
}
}
Así se ve nuestra gráfica:
No hay comentarios:
Publicar un comentario