@@ -23,6 +23,7 @@ namespace ExampleLibrary
2323 using OxyPlot . Axes ;
2424 using OxyPlot . Series ;
2525 using OxyPlot . Legends ;
26+ using System . Linq ;
2627
2728 [ Examples ( "Misc" ) ]
2829 public static class MiscExamples
@@ -239,19 +240,41 @@ public static PlotModel TrainSchedule()
239240 Title = "Train schedule" ,
240241 Subtitle = "Bergensbanen (Oslo-Bergen, Norway)" ,
241242 IsLegendVisible = false ,
242- PlotAreaBorderThickness = new OxyThickness ( 0 ) ,
243- PlotMargins = new OxyThickness ( 60 , 4 , 60 , 40 )
243+ PlotAreaBorderColor = OxyColors . LightGray ,
244244 } ;
245- model . Axes . Add (
246- new LinearAxis
247- {
248- Position = AxisPosition . Left ,
249- Minimum = - 20 ,
250- Maximum = 540 ,
251- Title = "Distance from Oslo S" ,
252- IsAxisVisible = true ,
253- StringFormat = "0"
254- } ) ;
245+
246+ var distanceAxis = new LinearAxis
247+ {
248+ Position = AxisPosition . Left ,
249+ Minimum = - 20 ,
250+ Maximum = 540 ,
251+ Title = "Distance from Oslo S" ,
252+ IsAxisVisible = true ,
253+ StringFormat = "0" ,
254+ } ;
255+
256+ model . Axes . Add ( distanceAxis ) ;
257+
258+ var stationAxis = new CustomAxis
259+ {
260+ MajorGridlineStyle = LineStyle . Solid ,
261+ MajorGridlineColor = OxyColors . LightGray ,
262+ Minimum = distanceAxis . Minimum ,
263+ Maximum = distanceAxis . Maximum ,
264+ Position = AxisPosition . Right ,
265+ IsPanEnabled = false ,
266+ IsZoomEnabled = false ,
267+ MajorTickSize = 0 ,
268+ } ;
269+
270+ distanceAxis . AxisChanged += ( sender , e ) =>
271+ {
272+ stationAxis . Minimum = distanceAxis . ActualMinimum ;
273+ stationAxis . Maximum = distanceAxis . ActualMaximum ;
274+ } ;
275+
276+ model . Axes . Add ( stationAxis ) ;
277+
255278 model . Axes . Add (
256279 new TimeSpanAxis
257280 {
@@ -315,23 +338,8 @@ public static PlotModel TrainSchedule()
315338 double x = double . Parse ( fields [ 1 ] , CultureInfo . InvariantCulture ) ;
316339 if ( ! string . IsNullOrEmpty ( fields [ 0 ] ) )
317340 {
318- // Add a horizontal annotation line for the station
319- model . Annotations . Add (
320- new LineAnnotation
321- {
322- Type = LineAnnotationType . Horizontal ,
323- Y = x ,
324- Layer = AnnotationLayer . BelowSeries ,
325- LineStyle = LineStyle . Solid ,
326- Color = OxyColors . LightGray ,
327- Text = fields [ 0 ] + " " ,
328- TextVerticalAlignment = VerticalAlignment . Middle ,
329- TextLinePosition = 1 ,
330- TextMargin = 0 ,
331- TextPadding = 4 ,
332- ClipText = false ,
333- TextHorizontalAlignment = HorizontalAlignment . Left
334- } ) ;
341+ stationAxis . MajorTicks . Add ( x ) ;
342+ stationAxis . Labels . Add ( fields [ 0 ] ) ;
335343 }
336344
337345 for ( int i = 0 ; i < series . Length ; i ++ )
@@ -389,23 +397,6 @@ public static PlotModel TrainSchedule()
389397 return model ;
390398 }
391399
392- /* [Example("World population")]
393- public static PlotModel WorldPopulation()
394- {
395- WorldPopulationDataSet dataSet;
396- using (var stream = GetResourceStream("WorldPopulation.xml"))
397- {
398- var serializer = new XmlSerializer(typeof(WorldPopulationDataSet));
399- dataSet = (WorldPopulationDataSet)serializer.Deserialize(stream);
400- }
401-
402- var model = new PlotModel { Title = "World population" };
403- model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Title = "millions" });
404- var series1 = new LineSeries { ItemsSource = dataSet.Items, DataFieldX = "Year", DataFieldY = "Population", StrokeThickness = 3, MarkerType = MarkerType.Circle };
405- model.Series.Add(series1);
406- return model;
407- }*/
408-
409400 [ Example ( "La Linea (AreaSeries)" ) ]
410401 public static PlotModel LaLineaAreaSeries ( )
411402 {
@@ -2382,21 +2373,23 @@ protected override int Solve(double x0, double y0, int maxIterations)
23822373 }
23832374 }
23842375
2385- /* [XmlRoot("DataSet")]
2386- [XmlInclude(typeof(Data))]
2387- public class WorldPopulationDataSet
2388- {
2389- [XmlElement("Data")]
2390- public List<Data> Items { get; set; }
2376+ private class CustomAxis : LinearAxis
2377+ {
2378+ public IList < double > MajorTicks { get ; } = new List < double > ( ) ;
2379+ public IList < double > MinorTicks { get ; } = new List < double > ( ) ;
2380+ public IList < string > Labels { get ; } = new List < string > ( ) ;
23912381
2392- public class Data
2393- {
2394- [XmlAttribute("Year")]
2395- public int Year { get; set; }
2382+ public override void GetTickValues ( out IList < double > majorLabelValues , out IList < double > majorTickValues , out IList < double > minorTickValues )
2383+ {
2384+ majorTickValues = majorLabelValues = this . MajorTicks . Where ( d => d >= this . ActualMinimum && d <= this . ActualMaximum ) . ToList ( ) ;
2385+ minorTickValues = this . MinorTicks . Where ( d => d >= this . ActualMinimum && d <= this . ActualMaximum ) . ToList ( ) ;
2386+ }
2387+
2388+ protected override string FormatValueOverride ( double x )
2389+ {
2390+ return this . Labels [ this . MajorTicks . IndexOf ( x ) ] ;
2391+ }
2392+ }
23962393
2397- [XmlAttribute("Population")]
2398- public double Population { get; set; }
2399- }
2400- }*/
24012394 }
24022395}
0 commit comments