Last active
May 27, 2020 21:44
-
-
Save joecastelo/7313a6f0465331890429dc7c7d316c38 to your computer and use it in GitHub Desktop.
Javscript Plot C# Script
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
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using VMS.TPS.Common.Model.API; | |
namespace VMS.TPS | |
{ | |
public class Script | |
{ | |
public static void Execute(ScriptContext context) | |
{ | |
var fields = context.PlanSetup.Beams.Where(e => !e.IsSetupField); //Not setup Fields | |
var MUS = fields.Select(e => e.Meterset.Value); // MUs | |
var SSDs = fields.Select(e => e.SSD);// SSDs in mm | |
var maxDoses = fields.Select(e => e.Dose.DoseMax3D.Dose);//Max doses in percentage | |
var createHTML = HTMLBuilder.StaticText(MUS, SSDs, maxDoses); | |
var runner = new HTMLRunner(createHTML); | |
runner.Launch("Test"); | |
} | |
} | |
public class HTMLRunner // Create HTML File | |
{ | |
public string Text { get; set; } | |
public string TempFolder { get; set; } | |
public HTMLRunner(string text) | |
{ | |
TempFolder = Path.GetTempPath(); | |
Text = text.ToString(); | |
} | |
public void Launch(string title) | |
{ | |
var fileName = Path.Combine(TempFolder, title + ".html"); | |
File.WriteAllText(fileName, Text); | |
System.Diagnostics.Process.Start(fileName); | |
} | |
} | |
public class HTMLBuilder //Generate the text for the plot to be passed to HTMLRunner class | |
{ | |
public static string StaticText(IEnumerable<double> xs, IEnumerable<double> ys, IEnumerable<double> zs) | |
{ | |
var preX = @" | |
<!DOCTYPE html> | |
<html lang='en'> | |
<head> | |
<meta charset=utf-8> | |
<title>Plot.ly 3D</title> | |
<script src='https://cdn.plot.ly/plotly-1.2.0.min.js'></script> | |
</head> | |
<body> | |
<h1>3D Plot</h1> | |
<div id='chart'> </div> | |
</body> | |
<script> | |
var chart = document.getElementById('chart'); | |
var data = { | |
"; | |
var x = "x:[" + string.Join(",", xs) + "],"; | |
var y = "y:[" + string.Join(",", ys) + "],"; | |
var z = "z:[" + string.Join(",", zs) + "],"; | |
var posY = @" | |
type: 'scatter3d' | |
}; | |
Plotly.newPlot(chart, [data], { | |
margin: { | |
t: 0 | |
} | |
}); | |
</script> | |
</html> | |
"; | |
return preX + x + y + z + posY; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment