【问题标题】:dynamicdatadisplay (wpf) how to make a string marker?dynamicdatadisplay (wpf) 如何制作字符串标记?
【发布时间】:2013-11-04 12:40:03
【问题描述】:

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

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

我有一个 X 轴,它是 DateTime(小时)。

我还有一个十进制的 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;
        }
    }

【问题讨论】:

  • 我建议您切换到另一个维护和记录的 WPF 图表组件。我一直在尝试 D3,这让我很头疼。

标签: c# wpf dynamic-data-display


【解决方案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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    相关资源
    最近更新 更多