Last active
November 11, 2015 15:53
-
-
Save davepape/f751601e0b163d6d97fc to your computer and use it in GitHub Desktop.
Plot some AirNow data as a line graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma strict | |
// Script to take AirNow AQI data and turn it into a line graph | |
// Data is assumed to be a time sequence for a single station, from the "Observations by Monitoring Site" query tool | |
// The data file should be in JSON format, and placed in a folder called "data" within the Assets folder | |
// This must be attached to a GameObject that has a Mesh Renderer and an empty Mesh Filter | |
// Using a Mesh Renderer, rather than a Line Renderer, allows us to set per-vertex colors. | |
import SimpleJSON; | |
function Start () | |
{ | |
// Categories start at 1, so entry 0 in the colormap is just a dummy value | |
var colormap = [ Color.white, Color.green, Color.yellow, Color(1.0,0.5,0.0), Color.red, Color(0.7,0,0.3) ]; | |
var i:int; | |
var myVertices = Array(); | |
var myColors = Array(); | |
var myTriangles = Array(); | |
var datastring = System.IO.File.ReadAllText("Assets/data/pm.json"); | |
var data = JSON.Parse(datastring); | |
for (i=0; i < data.AsArray.Count; i++) | |
{ | |
myVertices.push(Vector3(i,data[i]["AQI"].AsFloat/10.0,0)); | |
myColors.push(colormap[data[i]["Category"].AsInt]); | |
myVertices.push(Vector3(i,data[i]["AQI"].AsFloat/10.0+0.1,0)); | |
myColors.push(colormap[data[i]["Category"].AsInt]); | |
} | |
for (i=0; i < data.AsArray.Count-1; i++) | |
{ | |
myTriangles.push(i*2); | |
myTriangles.push(i*2+1); | |
myTriangles.push(i*2+2); | |
myTriangles.push(i*2+1); | |
myTriangles.push(i*2+3); | |
myTriangles.push(i*2+2); | |
} | |
var m = GetComponent.<MeshFilter>().mesh; | |
m.vertices = myVertices; | |
m.triangles = myTriangles; | |
m.colors = myColors; | |
m.RecalculateNormals(); | |
} | |
function Update() | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment