Skip to content

Instantly share code, notes, and snippets.

@iducool
Created January 15, 2016 06:57
Show Gist options
  • Save iducool/fb60f84469b1ae18fe00 to your computer and use it in GitHub Desktop.
Save iducool/fb60f84469b1ae18fe00 to your computer and use it in GitHub Desktop.
Date Graph issue
using System;
using System.Drawing;
using System.Collections.Generic;
using CoreGraphics;
using CoreText;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Security.Cryptography;
#if __UNIFIED__
using Foundation;
using UIKit;
#else
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using nint = global::System.Int32;
using nuint = global::System.UInt32;
#endif
using CorePlot;
using GraphViewTest;
namespace CorePlotiOSSample
{
// A convenience class to show samples
public class PlotViewController : UIViewController
{
public CPTGraphHostingView host;
public UIView graphView;
protected CPTGraph Graph;
// public override void ViewWillAppear(bool animated)
// {
// base.ViewWillAppear(animated);
//
// if (UIDevice.CurrentDevice.CheckSystemVersion(7, 0))
// EdgesForExtendedLayout = UIRectEdge.None;
//
// // Host the graph
//
// host = new CPTGraphHostingView(this.graphView.Bounds)
// {
// HostedGraph = Graph
// };
//
// View.AddSubview(host);
//
//
// }
}
public class ScatterPlot
{
CPTXYGraph graph;
public CPTGraphHostingView host;
public UIView graphView;
protected CPTGraph Graph;
public ScatterPlot(UIView graphView)
{
this.graphView = graphView;
SetupGraph();
SetupAxes();
SetupScatterPlots();
Graph = graph;
host = new CPTGraphHostingView(new RectangleF(20, 20, 300, 300))
{
HostedGraph = Graph
};
graphView.AddSubview(host);
}
void SetupGraph()
{
var theme = CPTTheme.ThemeNamed(CPTTheme.PlainWhiteTheme);
graph = new CPTXYGraph(new RectangleF(0, 0, 300, 300), CPTScaleType.Linear, CPTScaleType.Linear)
{
PaddingLeft = 10,
PaddingRight = 10,
PaddingTop = 10,
PaddingBottom = 10,
TitleTextStyle = new CPTMutableTextStyle
{
FontSize = 18,
FontName = "Helvetica",
Color = CPTColor.GrayColor
}
};
graph.BackgroundColor = CPTColor.ClearColor.Color;
//graph.ApplyTheme(theme);
}
void SetupAxes()
{
CPTXYPlotSpace plotspace = graph.DefaultPlotSpace as CPTXYPlotSpace;
//plotspace.AllowsMomentum = true;
plotspace.AllowsUserInteraction = true;
var major = new CPTLineStyle
{
LineWidth = .75f,
LineColor = CPTColor.WhiteColor.ColorWithAlphaComponent(1f)
};
var minor = new CPTLineStyle
{
LineWidth = .25f,
LineColor = CPTColor.WhiteColor.ColorWithAlphaComponent(0.1f)
};
var axisSet = (CPTXYAxisSet)graph.AxisSet;
// Label x with a fixed interval policy
var x = axisSet.XAxis;
x.LabelingPolicy = CPTAxisLabelingPolicy.Automatic;
x.LabelTextStyle.Color = CPTColor.WhiteColor;
x.AxisLineStyle.LineColor = CPTColor.WhiteColor;
//x.MinorTicksPerInterval = 4;
//x.PreferredNumberOfMajorTicks = 8;
x.MajorGridLineStyle = major;
x.MinorGridLineStyle = minor;
// x.Title = "X Axis";
x.TitleOffset = 0;
// Label y with an automatic label policy.
var y = axisSet.YAxis;
y.LabelingPolicy = CPTAxisLabelingPolicy.Automatic;
y.LabelTextStyle.Color = CPTColor.WhiteColor;
y.AxisLineStyle.LineColor = CPTColor.WhiteColor;
y.MinorTicksPerInterval = 4;
y.PreferredNumberOfMajorTicks = 8;
// y.MajorGridLineStyle = major;
y.MinorGridLineStyle = minor;
y.LabelOffset = 5;
//y.Title = "Y Axis";
y.TitleOffset = 0;
NSNumberFormatter yNumFormatter = new NSNumberFormatter();
yNumFormatter.NumberStyle = NSNumberFormatterStyle.Decimal;
yNumFormatter.MaximumFractionDigits = 3;
y.LabelFormatter = yNumFormatter;
NSDateFormatter dateFormatter = new NSDateFormatter();
dateFormatter.DateFormat = "dd/MM";
var timeFormatter = new CPTTimeFormatter(dateFormatter);
var component = new NSDateComponents();
component.Day = DateTime.Now.Day;
component.Month = DateTime.Now.Month;
component.Year = DateTime.Now.Year;
component.Hour = 05;
component.Minute = 30;
component.Second = 0;
component.TimeZone = NSTimeZone.SystemTimeZone;
var convertedDate = NSCalendar.CurrentCalendar.DateFromComponents(component);
timeFormatter.ReferenceDate = convertedDate;
x.LabelFormatter = timeFormatter;
plotspace.XRange = new CPTPlotRange(NSNumber.FromDouble(0).NSDecimalValue, NSNumber.FromDouble(86400 * 8).NSDecimalValue);
plotspace.YRange = new CPTPlotRange(NSNumber.FromDouble(0).NSDecimalValue, NSNumber.FromDouble(7).NSDecimalValue);
// x.GridLinesRange = new CPTPlotRange(NSNumber.FromDouble(0).NSDecimalValue, NSNumber.FromDouble(86400 * 8).NSDecimalValue);
// x.VisibleRange = new CPTPlotRange(NSNumber.FromDouble(0).NSDecimalValue, NSNumber.FromDouble(86400 * 8).NSDecimalValue);
}
public CPTScatterPlot dataSourceLinePlot{ get; set; }
void SetupScatterPlots()
{
// Create a plot that uses the data source method
dataSourceLinePlot = new CPTScatterPlot
{
DataSource = new RandomSamplesSource()//,
//PlotSymbolMarginForHitDetection = 5
};
var lineStyle = dataSourceLinePlot.DataLineStyle;
lineStyle.LineColor = CPTColor.WhiteColor;
lineStyle.LineWidth = 2;
dataSourceLinePlot.DataLineStyle = lineStyle;
graph.AddPlot(dataSourceLinePlot);
var plotSymbol = CPTPlotSymbol.EllipsePlotSymbol;
plotSymbol.Fill = CPTFill.FromColor(CPTColor.WhiteColor);
plotSymbol.LineStyle = new CPTLineStyle { LineColor = CPTColor.WhiteColor };
plotSymbol.Size = new CGSize(6, 6);
dataSourceLinePlot.PlotSymbol = plotSymbol;
}
}
public class RandomSamplesSource : CPTScatterPlotDataSource
{
List<GraphItem> _data;
public List<GraphItem> Data
{
get
{
if (_data == null)
{
_data = new List<GraphItem>();
_data.Add(new GraphItem { Day = 1, Rate = 1 });
_data.Add(new GraphItem { Day = 2, Rate = 2 });
_data.Add(new GraphItem { Day = 3, Rate = 3 });
_data.Add(new GraphItem { Day = 4, Rate = 4 });
_data.Add(new GraphItem { Day = 5, Rate = 5 });
_data.Add(new GraphItem { Day = 6, Rate = 6 });
_data.Add(new GraphItem { Day = 7, Rate = 7 });
}
return _data;
}
set { _data = value; }
}
public RandomSamplesSource()
{
}
public override nint NumberOfRecordsForPlot(CPTPlot plot)
{
return Data.Count;
}
public override NSNumber NumberForPlot(CPTPlot plot, CPTPlotField forFieldEnum, nuint index)
{
if (forFieldEnum == CPTPlotField.ScatterPlotFieldX)
return new NSNumber((index + 1) * 86400);
Debug.WriteLine("{0} with value:{1}", Data[(int)index].Rate, (index + 1) * 864000);
return Data[(int)index].Rate;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment