Xamarin.Forms |
|
Xamarin.Android |
|
Xamarin.iOS |
public class NutrientViewModel
{
public List<NutrientsClass> Data { get; set; }
public List<NutrientsClass> ColData { get; set; }
public NutrientResponse nutrientResponse { get; set; }
public NutrientViewModel()
{
nutrientResponse = NutritionOverview.nutrientResponse;
if (nutrientResponse != null)
{
Data = new List<NutrientsClass>()
{
new NutrientsClass { Nutrients = "calories", value =""+nutrientResponse.Content[0].calories },
new NutrientsClass { Nutrients = "carbs", value = ""+nutrientResponse.Content[0].carbs },
new NutrientsClass { Nutrients = "protein", value = ""+nutrientResponse.Content[0].protein },
new NutrientsClass { Nutrients = "fat", value= ""+nutrientResponse.Content[0].fat }
};
}
else {
Data = new List<NutrientsClass>()
{
new NutrientsClass { Nutrients = "calories", value ="20" },
new NutrientsClass { Nutrients = "carbs", value = "20" },
new NutrientsClass { Nutrients = "protein", value = "20" },
new NutrientsClass { Nutrients = "fat", value= "20"}
};
}
ColData = Data;
}
}
public class NutrientsClass
{
public double value { get; set; }
public string Nutrients { get; set; }
} |
<chart:ColumnSeries ItemsSource="{Binding Data}" Label="Nutrients" XBindingPath="Nutrients" YBindingPath="value" EnableTooltip="True">
<chart:ColumnSeries.DataMarker>
<chart:ChartDataMarker/>
</chart:ColumnSeries.DataMarker>
</chart:ColumnSeries> |
<chart:NumericalAxis x:Name="numericalAxis" >
. . .
<chart:NumericalAxis.LabelStyle>
<chart:ChartAxisLabelStyle LabelFormat="## kCal"/>
</chart:NumericalAxis.LabelStyle>
</chart:NumericalAxis> |
Hi Esperanza,Thanks for your patience.We have validated your chart code snippet, and we figured that you were bind string value for YBindingPath.We would like to let you know that the numeric axis will not support for string value, but it partially works when the value is able to convert as double.So, we suggested to convert the string value to double and bind it to the chart.[C#]
public class NutrientsClass{public double value { get; set; }public string Nutrients { get; set; }}[Xaml]
<chart:ColumnSeries ItemsSource="{Binding Data}" Label="Nutrients" XBindingPath="Nutrients" YBindingPath="value" EnableTooltip="True"><chart:ColumnSeries.DataMarker><chart:ChartDataMarker/></chart:ColumnSeries.DataMarker></chart:ColumnSeries>Note: Are you trying to add any string values to display it in chart axis? If yes, we can change the format of axis labels by using LabelFormat of LabelStyle class
<chart:NumericalAxis x:Name="numericalAxis" >. . .<chart:NumericalAxis.LabelStyle><chart:ChartAxisLabelStyle LabelFormat="## kCal"/></chart:NumericalAxis.LabelStyle></chart:NumericalAxis>Regards,Saravanan.