Skip to content

Instantly share code, notes, and snippets.

@michael-wolfenden
Created October 22, 2017 22:44
Show Gist options
  • Save michael-wolfenden/233e3aaaf9ad68aa530cd690c60d6f74 to your computer and use it in GitHub Desktop.
Save michael-wolfenden/233e3aaaf9ad68aa530cd690c60d6f74 to your computer and use it in GitHub Desktop.
Google charts - linqpad
<Query Kind="Program">
<Namespace>System.Globalization</Namespace>
</Query>
void Main()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); //Separate double digits with dot not comma - please!
var data = new[] {22.61, 22.59, 22.63, 22.60, 22.62, 22.58, 22.61, 22.62, 22.60, 22.64, 22.69, 22.62, 22.65, 22.62, 22.61, 22.65, 22.63, 22.61, 22.65, 22.63, 22.59, 22.62, 22.59, 22.64, 22.58, 22.61, 22.63, 22.61, 22.61, 22.69, 22.62, 22.59, 22.65, 22.65, 22.64, 22.63, 22.63, 22.63, 22.59, 22.61, 22.67, 22.57, 22.60, 22.61, 22.61, 22.61, 22.60, 22.61, 22.64, 22.62, 22.63, 22.67, 22.65, 22.65, 22.61, 22.63, 22.63, 22.61, 22.62, 22.69, 22.62, 22.59, 22.59, 22.61, 22.61, 22.58, 22.61, 22.65, 22.61, 22.63, 22.68, 22.62, 22.64, 22.62, 22.62, 22.64, 22.64, 22.63, 22.65, 22.61, 22.60, 22.63, 22.59, 22.62, 22.61, 22.59, 22.63, 22.62, 22.60, 22.69, 22.62, 22.61, 22.63, 22.65, 22.64, 22.63, 22.63, 22.65, 22.58, 22.60};
RenderLineGraph("Time", "P1", data);
}
public void RenderLineGraph(string horizontalAxisName, string verticalAxisName, IEnumerable<double> data)
{
var i = 0;
var jsData = "[" + string.Join(", ", data.Select(d => $"[{i++}, {d}]")) + "]";
Util.RawHtml($@"
<html>
<body>
<script type=""text/javascript"" src=""https://www.gstatic.com/charts/loader.js""></script>
<div id=""chart_div""></div>
<script>
google.charts.load('current', {{packages: ['corechart', 'line']}});
google.charts.setOnLoadCallback(drawBasic);
function drawBasic() {{
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', '{verticalAxisName}');
data.addRows({jsData});
var options = {{
hAxis: {{
title: '{horizontalAxisName}'
}},
vAxis: {{
title: '{verticalAxisName}'
}}
}};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}}
</script>
</body>
</html>
").Dump();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment