Skip to content

Instantly share code, notes, and snippets.

@recalde
Created November 13, 2024 03:48
Show Gist options
  • Save recalde/da1dd1967e04c4d45c05f1d29939d784 to your computer and use it in GitHub Desktop.
Save recalde/da1dd1967e04c4d45c05f1d29939d784 to your computer and use it in GitHub Desktop.
using ScottPlot;
using System;
using System.Collections.Generic;
using System.Linq;
var plt = new Plot(600, 400); // Fixed width, initial height
// Example data for bars
List<DateTime> startTimes = new List<DateTime>() { /* populate with start times */ };
List<DateTime> endTimes = new List<DateTime>() { /* populate with end times */ };
// Example times for vertical lines
List<DateTime> lineTimes = new List<DateTime>() { /* populate with times for vertical lines */ };
// Convert DateTime to double (OADate format used by ScottPlot)
double[] xs1 = startTimes.Select(t => t.ToOADate()).ToArray();
double[] xs2 = endTimes.Select(t => t.ToOADate()).ToArray();
double[] lineXPositions = lineTimes.Select(t => t.ToOADate()).ToArray();
// Set the Y values as row indices (1, 2, 3, ...)
double[] yPositions = Enumerable.Range(0, startTimes.Count).Select(i => (double)i).ToArray();
// Add bars to the plot
for (int i = 0; i < startTimes.Count; i++)
{
double width = xs2[i] - xs1[i];
plt.AddHorizontalBar(width, yPositions[i], xs1[i]);
}
// Add vertical dotted lines
foreach (var x in lineXPositions)
{
var vLine = plt.AddVerticalLine(x);
vLine.LineStyle = LineStyle.Dot;
vLine.Color = System.Drawing.Color.Gray; // Customize color if needed
}
// Adjust height based on row count
plt.Height = startTimes.Count * 30; // Adjust 30 based on row spacing
// Format x-axis as dates/times
plt.XAxis.DateTimeFormat(true);
plt.SaveFig("plot_with_lines.png"); // Save or display the plot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment