0

我正在为 WPF 使用动态数据显示库!不是银光

我已经完成了后端编码并坚持使用图表。

我有一个日期时间(小时)的 X 轴。

我还有一个十进制的 Y 轴。

但我找不到如何在这个轴自定义标记(或其他)之间放置一个动态字符串。

例如,在 03.11.2013 的 10 小时内(x 轴),股票价格为 120 美元(y 轴),但在图表中(在轴的交叉处)它必须是字符串“Volume: 50”,它将显示卖出了多少股票。

如何在代码中实现?请给我一些例子或建议。

现在我的项目看起来像这样:

========= XAML ==========

    <d3:ChartPlotter Name="plotter" Margin="20,20,20,70">
        <d3:ChartPlotter.HorizontalAxis>
            <d3:HorizontalDateTimeAxis Name="xAxis"/>
        </d3:ChartPlotter.HorizontalAxis>
        <d3:ChartPlotter.VerticalAxis>
            <d3:VerticalIntegerAxis Name="yAxis"/>
        </d3:ChartPlotter.VerticalAxis>
    </d3:ChartPlotter>

    <Button Content="Button" HorizontalAlignment="Left" Margin="254,364,0,0" VerticalAlignment="Top" Width="145" Height="46" Click="Button_Click_1"/>

</Grid>

==============================================

================ MainWindow.cs =================

private void Button_Click_1(object sender, RoutedEventArgs e)
    {

        List<DataForChart> dataForChart = new List<DataForChart>();

        dataForChart.Add(new DataForChart(new DateTime(2013, 11, 03, 22, 10, 0), 45));

        dataForChart.Add(new DataForChart(new DateTime(2013, 11, 03, 22, 20, 0), 48));

        dataForChart.Add(new DataForChart(new DateTime(2013, 11, 03, 22, 30, 0), 24));

        DateTime[] dates = new DateTime[dataForChart.Count];
        int[] price = new int[dataForChart.Count];

        for (int i = 0; i < dataForChart.Count; ++i)
        {
            dates[i] = dataForChart[i].date;
            price[i] = dataForChart[i].price;

        }

        var datesDataSource = new EnumerableDataSource<DateTime>(dates);
        datesDataSource.SetXMapping(x => xAxis.ConvertToDouble(x));

        var numberOpenDataSource = new EnumerableDataSource<int>(price);
        numberOpenDataSource.SetYMapping(y => y);

        CompositeDataSource compositeDataSource1 = new
        CompositeDataSource(datesDataSource, numberOpenDataSource);

        plotter.AddLineGraph(compositeDataSource1,
          new Pen(Brushes.Blue, 2),
          new CirclePointMarker { Size = 10.0, Fill = Brushes.Blue },
          new PenDescription("Price Chart"));

        plotter.Viewport.FitToView();

    }

    public class DataForChart
    {
        public DateTime date;
        public int price;

        public DataForChart(DateTime Date, int Price)
        {
            date = Date;
            price = Price;
        }
    }
4

1 回答 1

1
// Create a custom marker control as follows
// CustomMarker.xaml:
<d3:ViewportUIContainer x:Class="MyNamespace.CustomMarker"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:d3="clr-namespace:Microsoft.Research.DynamicDataDisplay.Charts;assembly=DynamicDataDisplay"
                    mc:Ignorable="d" >
   <Grid>
      <TextBlock Name="TxtBlk1" Text="Some Text" />
   </Grid>
</d3:ViewportUIContainer>

// Marker control code behind CustomMarker.cs
using System;
using System.Windows;
using System.Windows.Media;

namespace MyNamespace
{        
    public partial class CustomMarker : Microsoft.Research.DynamicDataDisplay.Charts.ViewportUIContainer 
    {
        public CustomMarker()
        {
            InitializeComponent();
        }

        public CustomMarker(Point position, string text, Color color) : this()
        {          
            Position = position;
            TxtBlk1.Text = text;
            TxtBlk1.Foreground = new SolidColorBrush(color);
        } 
    }
}


//In your mainwindow.cs
var position = new Point(x value, y value);
plotter.Children.Add(new CustomMarker(position, "Text", Colors.Black));
于 2014-02-06T20:17:45.540 回答