隋~~润泽 2021-12-11 00:37 采纳率: 66.7%
浏览 183
已结题

wpf live chart X轴总是显示两行为什么?怎么设置?

我添加了X轴想着用时间显示这个轴的信息,但是总是很乱不知道怎么设置,有会的吗?希望帮我一下谢谢
获取的时间没问题都是不重复的
图片

img

img

img

img

WPF.cs的代码


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BridgeHealthMonitoringSystem.Controller;
using BridgeHealthMonitoringSystem.Entity;
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Helpers;
using LiveCharts.Wpf;

namespace BridgeHealthMonitoringSystem.Form.DataManage
{

    /// <summary>
    /// 图表
    /// </summary>
    public partial class ChartWPF : Window
    {
        public SeriesCollection SeriesCollection { get; set; }

        ChuanGanQiController controller = null;
        public ChartWPF(List<string> numbers, List<string> sensorTypes, ChuanGanQiController controller)
        {
            InitializeComponent();
            numbers_cb.ItemsSource = numbers;
            cgqType_tb.ItemsSource = sensorTypes;
            numbers_cb.SelectedIndex = 0;
            cgqType_tb.SelectedIndex = 0;
            this.controller = controller;


            //SeriesCollection = new SeriesCollection
            //{
            //    new LineSeries
            //    {
            //        Values = new ChartValues<double> { -3.5, 5.25500, 7, 4 }
            //    },
            //     new ColumnSeries
            //    {
            //        Values = new ChartValues<decimal> { 5, 6, 2, 7 }
            //    }
            //};
            //DataContext = this;
            //liveChart1.Series = SeriesCollection;

        }




        /// <summary>
        /// 查看图表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            LoadShujv();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        public void LoadShujv() {

            try
            {
                

                List<ChuanGanQiEntity> entitys = controller.FilterCgq2();
                MessageBox.Show(entitys.Count+"","提示");
                StringBuilder stringBuilder = new StringBuilder();
                foreach (var item in entitys)
                {
                    stringBuilder.AppendLine(item.Data+"");
                }
                MessageBox.Show(stringBuilder.ToString(), "提示");

                if (SeriesCollection!=null)
                {
                    SeriesCollection.Clear();
                }
                SeriesCollection = new SeriesCollection();
                //编号分组 分不分都行 本身就是按一个查的
            

             //x坐标的内容
                List<string> xLabels = new List<string>();
            
                StringBuilder stringBuilder1 = new StringBuilder();
               
                   
                   //按日期分组
                    var groups2 = entitys.GroupBy(x => x.Time.ToString());
                    LineSeries lineSeries = new LineSeries();
                    lineSeries.Values = new ChartValues<double>();
                    List<ChuanGanQiEntity> entities1 = new List<ChuanGanQiEntity>();
                    foreach (var group2 in groups2) {
                   
                    //添加每组的第一个
                      var entities = group2.ToList();
                       var first= entities.FirstOrDefault();
                        entities1.Add(first);
                        
                    }
                    //按时间升序
                entities1= entities1.OrderBy(x => x.Time).ToList();
                int i = 0;

                entities1.ConvertAll<string>(x => x.Time.ToString());




                foreach (ChuanGanQiEntity chuanGanQiEntity in entities1)
                {
                    lineSeries.Values.Add( chuanGanQiEntity.Data);
                    xLabels.Add(chuanGanQiEntity.Time.ToString());
                    stringBuilder1.AppendLine(chuanGanQiEntity.Time.ToString());
                    i++;
                }



                //线条显示数值
                lineSeries.Title = "数据";
                    lineSeries.DataLabels = true;
                    lineSeries.LabelPoint = x => x.Y+"k";

              
                    SeriesCollection.Add(lineSeries);
                  
                

              MessageBox.Show(stringBuilder1.ToString());

                //    SeriesCollection = new SeriesCollection
                //{
                //    new LineSeries
                //    {
                //        Values = new ChartValues<double> { 3, 5, 7, 4 }
                //    },
                //     new ColumnSeries
                //    {
                //        Values = new ChartValues<decimal> { 5, 6, 2, 7 }
                //    }
                //};~


                liveChart1.Series.Clear();

                liveChart1.Series = SeriesCollection;
              
                //图列的显示位置
                liveChart1.LegendLocation = LegendLocation.Top;

                //   liveChart1.AxisX.FirstOrDefault().LabelFormatter = "D";
                Func<double, string> xFomat = val => new DateTime((long)val).ToString("yyyy-MM-dd hh:mm:ss");
                liveChart1.AxisX.Add(new Axis()
                {
                 
                  Labels =xLabels.ToArray(),
                    //   LabelFormatter = xFomat,
                    Separator = new LiveCharts.Wpf.Separator // force the separator step to 1, so it always display all labels
                    {
                        StrokeThickness=1,
                        Step =1
                    

                        //   IsEnabled = false //disable it to make it invisible.
                    },
                    ShowLabels =true,
                    LabelsRotation = 30,
                    FontSize = 15,
                    Title = "日期时间",
                    Position=AxisPosition.LeftBottom
                   
                });


                //liveChart1.AxisY.Add(new Axis
                //{
                //    LabelFormatter = value => value + ".00K items",
                //    Separator = new LiveCharts.Wpf.Separator()
                //});

                DataContext = this;

            }
            catch (Exception ex)
            {

                MessageBox.Show(string.Format("{0}\n{1}\n{2}\n", ex.Message, ex.Source, ex.StackTrace));
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           List<ChuanGanQiEntity> entitys = controller.FilterCgq2();


           LoadShujv();
        }

        private void liveChart1_DataClick(object sender, ChartPoint point)
        {
            MessageBox.Show(sender.ToString());
            MessageBox.Show("You clicked " + point.X + ", " + point.Y);
          
        }
    }
}

xaml的代码

<Window x:Class="BridgeHealthMonitoringSystem.Form.DataManage.ChartWPF"
             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:local="clr-namespace:BridgeHealthMonitoringSystem.Form.DataManage"
        xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
                  xmlns:ctrl="clr-namespace:WPF_DateTimePicker.UserControls.DateTimePicker.View"
             mc:Ignorable="d" 
             d:DesignHeight="1080" d:DesignWidth="1250" Loaded="Window_Loaded" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="8*"></ColumnDefinition>
            <ColumnDefinition Width="2*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        
        
        <lvc:CartesianChart Name="liveChart1"  Grid.Column="0" DataClick="liveChart1_DataClick"/>



        <DockPanel Grid.Column="1">
                
            
            <StackPanel DockPanel.Dock="Top">
                <Label>传感器类型</Label>
                <ComboBox Name="cgqType_tb"></ComboBox>
                <Label>监测点编号</Label>
                <ComboBox Name="numbers_cb"></ComboBox>
            </StackPanel>

                <StackPanel DockPanel.Dock="Bottom">
                <Label>监测日期</Label>
                <ctrl:DateTimePicker BorderBrush="Gray" Height="35" Width="120"  Margin="10,0,0,0" x:Name="timeone"></ctrl:DateTimePicker>
                <Label></Label>
                <ctrl:DateTimePicker BorderBrush="Gray" Height="35" Width="120"  Margin="10,0,0,0" x:Name="timeone2"></ctrl:DateTimePicker>
                <Button Content="查看图表" Click="Button_Click"></Button>
                    
                </StackPanel>
            </DockPanel>
            
            
       
        
    </Grid>
</Window>


  • 写回答

1条回答 默认 最新

  • 隋~~润泽 2021-12-11 22:50
    关注

    经过我排查 发现是添加X轴的时候上次的没清除的原因 终于解决了!!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月19日
  • 已采纳回答 12月11日
  • 创建了问题 12月11日

悬赏问题

  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 帅锅们,我很想知道这个论坛是用什么模板搭的(T_T)
  • ¥15 请问如何查看手机root记录?
  • ¥15 商城小程序订单号重复
  • ¥15 学校优化算法sbo和蚁群算法怎么结合
  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素