【问题标题】:how to draw horizontal and vertical lines in interval bar chart - oxyplot - WPF如何在间隔条形图中绘制水平线和垂直线 - oxyplot - WPF
【发布时间】:2018-01-29 18:45:27
【问题描述】:

下午好,现在我有这个图表,但我想在条形图中添加红线和蓝线。我很新withwith oxyplot。感谢您的帮助。

我目前正在处理保存为布尔值的继电器中的事件显示。所以最好有水平线参考。

蓝线只是代表系统中事件的另一条线。

这是我的 xaml

<Window x:Class="Label_Issue.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Label_Issue"
    xmlns:oxy="http://oxyplot.org/wpf"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <oxy:PlotView x:Name="barChartModel"/>
</Grid>

这是我的 .cs

namespace Label_Issue
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            SetUtilizationData();
        }

        public PlotModel PlotModel { get; set; }
        private void SetUtilizationData()
        {
            PlotModel = new PlotModel
            {

                LegendOrientation = LegendOrientation.Vertical,
                LegendPlacement = LegendPlacement.Outside,
                LegendPosition = LegendPosition.RightTop
            };


            // define x-axis
            OxyPlot.Axes.DateTimeAxis dateAxis = new OxyPlot.Axes.DateTimeAxis
            {
                Position = OxyPlot.Axes.AxisPosition.Bottom,
                //StringFormat = "dd/MM/yy HH:mm"         // automatisch?
            };

            // add to plotmodel.axes
            PlotModel.Axes.Add(dateAxis);


            // define y-axis
            CategoryAxis categoryAxis = new CategoryAxis
            {
                Position = AxisPosition.Left,
            };

            //add to plotmodel.axes
            PlotModel.Axes.Add(categoryAxis);

            IntervalBarSeries barSeries = new OxyPlot.Series.IntervalBarSeries
            {
                LabelMargin = 0
            };

            TestData td = new TestData();
            for(int index=0; index<10;index++ )
            {
                IntervalBarItem item = new IntervalBarItem
                {

                    Start = OxyPlot.Axes.DateTimeAxis.ToDouble(new DateTime(2017, 04, 01, 06, 00 + index, 00)),
                    End = OxyPlot.Axes.DateTimeAxis.ToDouble(new DateTime(2017, 04, 01, 07, 00 + index, 00)),
                    CategoryIndex = index,
                    Title = "test"
                };
                barSeries.Items.Add(item);
            }

            PlotModel.Series.Add(barSeries);
            barChartModel.Model = PlotModel;
        }
    }

【问题讨论】:

    标签: c# wpf bar-chart oxyplot


    【解决方案1】:

    我在 Y 轴上用 Plotmodel.LineAannotation 制作了垂直线,在 Y 轴上用 majorgridlinestyle 制作了水平线,但也可以用线条注释来完成。

    【讨论】:

      【解决方案2】:

      对于其他人:

      using OxyPlot.Annotations;
      
      double X = 0.0D;
      double Y = 0.87825D;
      
      LineAnnotation Line = new LineAnnotation()
      {
          StrokeThickness = 1,
          Color = OxyColors.Green,
          Type = LineAnnotationType.Horizontal,
          Text = (Y).ToString(),
          TextColor = OxyColors.White,
          X = X,
          Y = Y
          };
      
      myPlotViewModel.Annotations.Add(Line);
      

      【讨论】: