Skip to content

Commit

Permalink
[feat] 添加更多图表
Browse files Browse the repository at this point in the history
Resolve #33
  • Loading branch information
zxbmmmmmmmmm committed May 4, 2024
1 parent 502689f commit 16299d2
Show file tree
Hide file tree
Showing 17 changed files with 404 additions and 40 deletions.
4 changes: 3 additions & 1 deletion FluentWeather.Abstraction/Models/WeatherHourlyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace FluentWeather.Abstraction.Models;

public class WeatherHourlyBase :WeatherBase, ITemperature, ITime, IWind, IHumidity, IPressure,IPrecipitationProbability
public class WeatherHourlyBase : WeatherBase, ITemperature, ITime, IWind, IHumidity, IPressure, IPrecipitationProbability, ICloudAmount, IVisibility
{
public int? Humidity { get; set; }
public int? Pressure { get; set; }
Expand All @@ -23,4 +23,6 @@ public string WindDirectionDescription
public int WindSpeed { get; set; }
public int? PrecipitationProbability { get; set; }
public WindDirection WindDirection { get; set; }
public int? CloudAmount { get ; set ; }
public int? Visibility { get ; set ; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentWeather.Abstraction.Helpers;
using FluentWeather.Abstraction.Models;
using FluentWeather.OpenMeteoProvider.Models;
using OpenMeteoApi.Models;
using System;
Expand All @@ -7,9 +8,9 @@ namespace FluentWeather.OpenMeteoProvider.Mappers;

public static class HourlyForecastMapper
{
public static OpenMeteoHourlyForecast MapToOpenMeteoHourlyForecast(this HourlyForecastItem item)
public static WeatherHourlyBase MapToHourlyForecast(this HourlyForecastItem item)
{
return new OpenMeteoHourlyForecast
return new WeatherHourlyBase
{
WeatherType = WeatherCodeHelper.GetWeatherType(item.WeatherCode!.Value),
WindDirection = UnitConverter.GetWindDirectionFromAngle(item.WindDirection10m!.Value),
Expand All @@ -21,7 +22,8 @@ public static OpenMeteoHourlyForecast MapToOpenMeteoHourlyForecast(this HourlyFo
Temperature = (int)item.Temperature2m!,
Time = item.Time!.Value,
PrecipitationProbability = item.PrecipitationProbability,
CloudAmount = item.CloudCover
CloudAmount = item.CloudCover,
Visibility = (int?)item.Visibility/1000,
};
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion FluentWeather.OpenMeteoProvider/OpenMeteoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<List<WeatherDailyBase>> GetDailyForecasts(double lon, double l
public async Task<List<WeatherHourlyBase>> GetHourlyForecasts(double lon, double lat)
{
var result = await Client.GetHourlyForecasts(lat, lon);
return result.ConvertAll<WeatherHourlyBase>(p => p.MapToOpenMeteoHourlyForecast());
return result.ConvertAll<WeatherHourlyBase>(p => p.MapToHourlyForecast());
}

public async Task<PrecipitationBase> GetPrecipitations(double lon, double lat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@
<Compile Include="Mappers\WeatherWarningMapper.cs" />
<Compile Include="Models\QAirCondition.cs" />
<Compile Include="Models\QTyphoon.cs" />
<Compile Include="Models\QWeatherHourlyForecast.cs" />
<Compile Include="Models\QWeatherIndices.cs" />
<Compile Include="Models\QWeatherNight.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentWeather.Abstraction.Helpers;
using FluentWeather.Abstraction.Models;
using FluentWeather.Uwp.QWeatherProvider.Helpers;
using FluentWeather.Uwp.QWeatherProvider.Models;
using QWeatherApi.ApiContracts;
Expand All @@ -7,9 +8,9 @@ namespace FluentWeather.Uwp.QWeatherProvider.Mappers;

public static class HourlyForecastItemMapper
{
public static QWeatherHourlyForecast MapToQWeatherHourlyForecast(this WeatherHourlyResponse.HourlyForecastItem item)
public static WeatherHourlyBase MapToHourlyForecast(this WeatherHourlyResponse.HourlyForecastItem item)
{
return new QWeatherHourlyForecast
return new WeatherHourlyBase
{
WeatherType = WeatherTypeConverter.GetWeatherTypeByIcon(int.Parse(item.Icon)),
WindDirection = UnitConverter.GetWindDirectionFromAngle(item.Wind360),
Expand All @@ -22,7 +23,7 @@ public static QWeatherHourlyForecast MapToQWeatherHourlyForecast(this WeatherHou
Temperature = item.Temp,
Time = item.FxTime,
PrecipitationProbability = item.Pop,
CloudAmount = item.Cloud
CloudAmount = item.Cloud,
};
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion FluentWeather.Uwp.QWeatherProvider/QWeatherProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public async Task<List<WeatherDailyBase>> GetDailyForecasts(double lon, double l
public async Task<List<WeatherHourlyBase>> GetHourlyForecasts(double lon, double lat)
{
var result = await RequestAsync(QWeatherApis.WeatherHourlyApi, new QWeatherRequest(lon, lat));
var res = result.HourlyForecasts?.ConvertAll(p => (WeatherHourlyBase)p.MapToQWeatherHourlyForecast());
var res = result.HourlyForecasts?.ConvertAll(p => (WeatherHourlyBase)p.MapToHourlyForecast());
return res;
}

Expand Down
1 change: 1 addition & 0 deletions FluentWeather.Uwp/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<valueConverters:SeverityColorToColorConverter x:Key="SeverityColorToColorConverter" />
<valueConverters:ToShortTimeStringConverter x:Key="ToShortTimeStringConverter" />
<valueConverters:WindScaleToDescriptionConverter x:Key="WindScaleToDescriptionConverter" />
<valueConverters:ListPropertyNullToVisibilityConverter x:Key="ListPropertyNullToVisibilityConverter" />
</ResourceDictionary>
</Application.Resources>
</Application>
89 changes: 89 additions & 0 deletions FluentWeather.Uwp/Controls/HourlyDataChart.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<UserControl
x:Class="FluentWeather.Uwp.Controls.HourlyDataChart"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:chart="using:Telerik.UI.Xaml.Controls.Chart"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:FluentWeather.Uwp.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:valueConverters="using:ValueConverters"
d:DesignHeight="300"
d:DesignWidth="400"
mc:Ignorable="d">

<UserControl.Resources>
<valueConverters:AddConverter x:Key="AddConverter" />
<DataTemplate x:Key="PointTemplate">
<Ellipse
Width="8"
Height="8"
Fill="{ThemeResource SystemControlHighlightAltAltHighBrush}"
Stroke="{ThemeResource SystemControlBackgroundBaseMediumHighBrush}"
StrokeThickness="2" />
</DataTemplate>
</UserControl.Resources>
<Grid>
<chart:RadCartesianChart>
<chart:RadCartesianChart.HorizontalAxis>
<chart:CategoricalAxis
x:Uid="TimeAxis"
LabelFormat="{}{0,0:HH:00}"
MajorTickInterval="3"
PlotMode="OnTicksPadded" />
</chart:RadCartesianChart.HorizontalAxis>
<chart:RadCartesianChart.VerticalAxis>
<chart:LinearAxis
x:Name="VerticalLinearAxis"
HorizontalLocation="Left"
LabelFormat="{x:Bind LabelFormat, Mode=OneWay}">
<chart:LinearAxis.LineStyle>
<Style TargetType="Line">
<Setter Property="Stroke" Value="Transparent" />
</Style>
</chart:LinearAxis.LineStyle>
<chart:LinearAxis.MajorTickStyle>
<Style TargetType="Rectangle">
<Setter Property="Stroke" Value="Transparent" />
</Style>
</chart:LinearAxis.MajorTickStyle>
</chart:LinearAxis>
</chart:RadCartesianChart.VerticalAxis>

<chart:RadCartesianChart.Grid>
<chart:CartesianChartGrid MajorLinesVisibility="Y" />
</chart:RadCartesianChart.Grid>
<chart:SplineAreaSeries
chart:ChartTrackBallBehavior.IntersectionTemplate="{x:Bind IntersectionTemplate, Mode=OneWay}"
chart:ChartTrackBallBehavior.TrackInfoTemplate="{x:Bind TrackInfoTemplate, Mode=OneWay}"
ClipToPlotArea="False"
CombineMode="Cluster"
ItemsSource="{x:Bind HourlyForecasts, Mode=OneWay}"
Stroke="{ThemeResource SystemControlBackgroundBaseHighBrush}"
StrokeThickness="3">
<chart:SplineAreaSeries.Fill>
<LinearGradientBrush Opacity="0.4" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="{ThemeResource SystemBaseHighColor}" />
<GradientStop Offset="1" Color="Transparent" />
</LinearGradientBrush>
</chart:SplineAreaSeries.Fill>
<chart:SplineAreaSeries.LabelDefinitions>
<chart:ChartSeriesLabelDefinition
Margin="0,8,0,0"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</chart:SplineAreaSeries.LabelDefinitions>
<chart:SplineAreaSeries.CategoryBinding>
<chart:PropertyNameDataPointBinding PropertyName="Time" />
</chart:SplineAreaSeries.CategoryBinding>
<chart:SplineAreaSeries.ValueBinding>
<chart:PropertyNameDataPointBinding PropertyName="{x:Bind PropertyName, Mode=OneWay}" />
</chart:SplineAreaSeries.ValueBinding>
</chart:SplineAreaSeries>
<chart:RadCartesianChart.Behaviors>
<chart:ChartTrackBallBehavior ShowInfo="False" ShowIntersectionPoints="True" />
</chart:RadCartesianChart.Behaviors>

</chart:RadCartesianChart>

</Grid>
</UserControl>
124 changes: 124 additions & 0 deletions FluentWeather.Uwp/Controls/HourlyDataChart.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using FluentWeather.Abstraction.Models;
using Telerik.UI.Xaml.Controls.Chart;

//https://go.microsoft.com/fwlink/?LinkId=234236 上介绍了“用户控件”项模板

namespace FluentWeather.Uwp.Controls
{
public sealed partial class HourlyDataChart : UserControl
{
public HourlyDataChart()
{
this.InitializeComponent();
}

public List<WeatherHourlyBase> HourlyForecasts
{
get => ((List<WeatherHourlyBase>)GetValue(HourlyForecastsProperty));
set => SetValue(HourlyForecastsProperty, value);
}


public static readonly DependencyProperty HourlyForecastsProperty =
DependencyProperty.Register(nameof(HourlyForecasts), typeof(List<WeatherHourlyBase>), typeof(HourlyDataChart), new PropertyMetadata(default));

public string PropertyName
{
get => (string)GetValue(PropertyNameProperty);
set => SetValue(PropertyNameProperty, value);
}

public static readonly DependencyProperty PropertyNameProperty =
DependencyProperty.Register(nameof(PropertyName), typeof(string), typeof(HourlyDataChart), new PropertyMetadata(default));


public double Minimum
{
get => (double)GetValue(MinimumProperty);
set => SetValue(MinimumProperty, value);
}

public static readonly DependencyProperty MinimumProperty =
DependencyProperty.Register(nameof(Minimum), typeof(double), typeof(HourlyDataChart), new PropertyMetadata(default, OnPropertyChanged));

public double Maximum
{
get => (double)GetValue(MaximumProperty);
set => SetValue(MaximumProperty, value);
}

public static readonly DependencyProperty MaximumProperty =
DependencyProperty.Register(nameof(Maximum), typeof(double), typeof(HourlyDataChart), new PropertyMetadata(default, OnPropertyChanged));

public double MajorStep
{
get => (double)GetValue(MajorStepProperty);
set => SetValue(MajorStepProperty, value);
}

public static readonly DependencyProperty MajorStepProperty =
DependencyProperty.Register(nameof(MajorStep), typeof(double), typeof(HourlyDataChart), new PropertyMetadata(default, OnPropertyChanged));

public DataTemplate TrackInfoTemplate
{
get => (DataTemplate)GetValue(TrackInfoTemplateProperty);
set => SetValue(TrackInfoTemplateProperty, value);
}

public static readonly DependencyProperty TrackInfoTemplateProperty =
DependencyProperty.Register(nameof(TrackInfoTemplateProperty), typeof(DataTemplate), typeof(HourlyDataChart), new PropertyMetadata(default));

public DataTemplate IntersectionTemplate
{
get => (DataTemplate)GetValue(IntersectionTemplateProperty);
set => SetValue(IntersectionTemplateProperty, value);
}

public static readonly DependencyProperty IntersectionTemplateProperty =
DependencyProperty.Register(nameof(IntersectionTemplateProperty), typeof(DataTemplate), typeof(HourlyDataChart), new PropertyMetadata(default));

public string LabelFormat
{
get => (string)GetValue(LabelFormatProperty);
set => SetValue(LabelFormatProperty, value);
}

public static readonly DependencyProperty LabelFormatProperty =
DependencyProperty.Register(nameof(LabelFormat), typeof(string), typeof(HourlyTemperatureChart), new PropertyMetadata(default));



private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var chart = (HourlyDataChart)d;
//chart.Bindings.Update();
if (e.NewValue is default(double)) return;
if(e.Property == MinimumProperty)
{
chart.VerticalLinearAxis.Minimum = (double)e.NewValue;
}
if (e.Property == MaximumProperty)
{
chart.VerticalLinearAxis.Maximum = (double)e.NewValue;
}
if (e.Property == MajorStepProperty)
{
chart.VerticalLinearAxis.MajorStep = (double)e.NewValue;
}
}
}
}
2 changes: 1 addition & 1 deletion FluentWeather.Uwp/Controls/HourlyTemperatureChart.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</chart:CategoricalAxis>
</chart:RadCartesianChart.HorizontalAxis>
<chart:RadCartesianChart.VerticalAxis>
<chart:LinearAxis
<chart:LinearAxis
LabelFormat="{}{0}°"
Maximum="{x:Bind MaxTemperature}"
Minimum="{x:Bind MinTemperature}">
Expand Down
6 changes: 3 additions & 3 deletions FluentWeather.Uwp/Controls/HourlyTemperatureChart.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public HourlyTemperatureChart()
}
public List<WeatherHourlyBase> HourlyForecasts
{
get => ((List<WeatherHourlyBase>)GetValue(WeatherForecastsProperty));
set => SetValue(WeatherForecastsProperty, value);
get => ((List<WeatherHourlyBase>)GetValue(HourlyForecastsProperty));
set => SetValue(HourlyForecastsProperty, value);
}


public static readonly DependencyProperty WeatherForecastsProperty =
public static readonly DependencyProperty HourlyForecastsProperty =
DependencyProperty.Register(nameof(HourlyForecasts), typeof(List<WeatherHourlyBase>), typeof(HourlyTemperatureChart), new PropertyMetadata(default, OnPropertyChanged));
public bool IsHorizontalAxisVisible
{
Expand Down
8 changes: 8 additions & 0 deletions FluentWeather.Uwp/FluentWeather.Uwp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@
<Compile Include="Controls\Dialogs\UpdateDialog.xaml.cs">
<DependentUpon>UpdateDialog.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\HourlyDataChart.xaml.cs">
<DependentUpon>HourlyDataChart.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\HourlyTemperatureChart.xaml.cs">
<DependentUpon>HourlyTemperatureChart.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -229,6 +232,7 @@
<Compile Include="Helpers\ValueConverters\ConverterMethods.cs" />
<Compile Include="Helpers\ValueConverters\DataConverters.cs" />
<Compile Include="Helpers\ValueConverters\DateConverter.cs" />
<Compile Include="Helpers\ValueConverters\ListPropertyNullToVisibilityConverter.cs" />
<Compile Include="Helpers\ValueConverters\VisibilityInverter.cs" />
<Compile Include="Helpers\XamlRenderService.cs" />
<Compile Include="Pages\CitiesPage.xaml.cs">
Expand Down Expand Up @@ -483,6 +487,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Controls\HourlyDataChart.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Controls\HourlyTemperatureChart.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
Loading

0 comments on commit 16299d2

Please sign in to comment.