0

I'm using the WinRTXamlToolkit.Controls.DataVisualization.Charting.Chart object, with a dependent axis integer of 0/1, and an independent axis of time. I'd like to suppress or perhaps rotate the labels at the top of the chart. Are the styles found on the Axis (chart.Axes) or series (LineSeries)? My chart is completely configured through code, snippets below:

EDIT 1/30/2017-3: added hosting XAML page.

<Page
    x:Class="HomeControl.Views.Historical"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:Charting="using:WinRTXamlToolkit.Controls.DataVisualization.Charting"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:HomeControl.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Charting:Chart x:Name="LineChart" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalAlignment="Top" Height="500">
        </Charting:Chart>
    </Grid>
</Page>

EDIT 1/30/2017-2: added remaining code...

var lowDate = records.First().taken.DateTime;
var highDate = records.Last().taken.DateTime;

var allDeviceTelemetry = records.GroupBy(t => t.sensorid).OrderBy(g => g.Key);

var axisTaken = new DateTimeAxis()
    {
        Title = "Taken",
        Orientation = AxisOrientation.X,
        IntervalType = DateTimeIntervalType.Minutes,
        Interval = 5,
        Minimum = lowDate,
        Maximum = highDate,
};
LineChart.Axes.Add(axisTaken);

LineChart.Axes.Add(new LinearAxis()
{
    Title = "State",
    Orientation = AxisOrientation.Y
});

foreach (var deviceTelemetry in allDeviceTelemetry)
{
    var series = new LineSeries()
    {
        Title = deviceTelemetry.Key, // sensorid
        IndependentValuePath = "taken",
        DependentValuePath = "sensorvalueint",
        ItemsSource = deviceTelemetry
    };
    LineChart.Series.Add(series);
}

The area I'm trying to control is in green: Sample Line Chart

I've played around with some of the other styles, such as the interval and axis titles, I just can't figure out where the data point label styles are?

EDIT 1/30/2017: Here is the tree, with the highlighted object (TextBlock at bottom). I need to figure out how to style this "AxisLabel", "Panel", "AxisGrid" or "CategoryAxis" - through code. enter image description here

Any hints would be appreciated!

-John

Xie Steven
  • 8,544
  • 1
  • 9
  • 23
JohnKoz
  • 908
  • 11
  • 21
  • You have to set the `AxisLabelStyle` property of your `DateTimeAxis`. But remember: this is the **XAML** toolkit, you're supposed to be doing it in... well, XAML... – jsanalytics Jan 30 '17 at 01:18
  • Thanks jstreet, but unfortunately that's ironically the first style I tried (AxisLabelStyle), and that only affected the BOTTOM (axis, 8:15, 8:20, etc.) labels, not the top. – JohnKoz Jan 30 '17 at 04:11
  • Did you assign the style to the top axis? – jsanalytics Jan 30 '17 at 04:13
  • Hi jstreet. I edited my post, adding a visual tree. I think **styling the top axis** is the spirit of my question, do you have any guidance on how to do that programmatically? – JohnKoz Jan 30 '17 at 15:03
  • Given that you mentioned that the top axis was _unexpected_ for you, i would think you might want to find a way to remove it, not to style it. But in any case you should post the full code that yield that chart. People can get a picture of the visual tree themselves but cannot get your code. Finally, in case you haven't done it yet, please read [MCVE](http://stackoverflow.com/help/mcve). – jsanalytics Jan 30 '17 at 15:12
  • jstreet, I thought I'd omit a few lines of code that I didn't think were relevant, but to your suggestion I've edited my post with the missing lines; hope that helps. BTW, yes, I eliminating the 'unexpected' axis was among the approaches I was investigating, but without success. – JohnKoz Jan 30 '17 at 15:24
  • Is that code inside a method? Is there a XAML file corresponding to that code behind? _"Floating"_ code like that makes it difficult to figure out things. – jsanalytics Jan 30 '17 at 15:31
  • TIP: in general, when addressing someone specifically, you should use `@someusername`, otherwise people may not necessarily see your comments. – jsanalytics Jan 30 '17 at 15:33
  • Thanks again @jstreet, I've edited the post to include the XAML, I originally omitted it because it was trivial. At this point do you have any specific suggestions on the subject matter? – JohnKoz Jan 30 '17 at 15:51
  • I suspect that, because you're adding you series through a `for` loop, the first 2 axes are associated with the first series. And then, for the second series, it doesn't know what to do and adds that "extra" category axis. That wouldn't happen in XAML, but it's just a guess at this point. – jsanalytics Jan 30 '17 at 16:02
  • I ran your code and it did NOT reproduce the problem you're describing. – jsanalytics Jan 30 '17 at 16:33
  • Also check https://github.com/beto-rodriguez/Live-Charts and https://github.com/telerik/UI-For-UWP – Filip Skakun Feb 08 '17 at 18:35

2 Answers2

1

Not a complete answer, since I can't tell how you made labels to show up at the top, but these seem to me like they are more of data point labels rather than axis labels. Running the toolkit's sample app might help you browse the UI tree a bit and make it easier to explore things. Just get the toolkit's source in your VS, hit F5, open the Chart control sample and hit Ctrl+Shift while pointing at a label. Here's what I get when pointing at the category axis:

enter image description here

I would poke around the data points, the series and search for a property called style. Otherwise - browse the source code to find how it gets set up.

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
  • Thanks Filip, yes I've studied the visual tree already for my output, I've edited the original post to include it for your reference. I need guidance on how to style the CategoryAxis>AxisGrid>Panel>AxisLabel>TextBlock through **code**. The CategoryAxis was unexpected for me, I did not explicitly add it. – JohnKoz Jan 30 '17 at 11:57
  • Well, I see you adding a DateTimeAxis there and I suppose that doesn't remove the CategoryAxis that's already there. I'd have to agree with jstreet about [MVCE](http://stackoverflow.com/help/mcve). Try to create as small a set of code files as you can paste in SO that when added to a new app project reproduce your problem. – Filip Skakun Jan 30 '17 at 20:12
0

I've solved my issue, but not with an outcome I expected - much better.

After experimenting quite a bit, I've learned a few things about the WinRTXAML charting; these observations are from purely a coding perspective, since I am not using static XAML in my page. I am new to the control, so if anyone knows these learnings to be incomplete or misguided please chime in:

  • Axes are created dynamically, based on the Series being added, if an appropriate Axis is not predefined (in the Axes property). This creation of axes does not occur until data binding takes place (understandably because the chart needs to reflect on the datatypes of the incoming data).
  • The explicit assignment of a series to an axis is achieved through the IndependentAxis and DependentRangeAxis of the Series object. If this is omitted, and assignment will be made implicitly based on the data type of the series bound Value. Conversely, if an axis explicitly assigned to a series it need not be added to the Axes property on the Chart (but it also can be).
  • If an axis is explicitly assigned to a series which conflicts with the datatype of that series, a horribly misleading exception is generated. "Assigned independent axis cannot be used. This may be due to an unset Orientation property for the axis."
  • A CategoryAxis is automatically generated for a series value of type string, but also when the datatype does not match an existing axis. (I.e. the datatype is cast to a string, then a CategoryAxis is produced/used)

Applying these learnings to my original issue, here's what happened. Although I had a DateTime axis predefined, my incoming datatype for the independent axis was DateTimeOffset. This value was interpreted as a string value, since it was not DateTime (i.e. no implicit conversion). This caused the Chart to generated a CategoryAxis, assign it to the series, and it placed it at the top of the chart. Not understanding this was happening, I didn't want the labels on this top axis, so I was trying to suppress them, but I could not find the axis, since it was not created until AFTER data binding took place.

SOLUTION: make the "taken" value datatype DateTime, this caused the chart to align [explicitly or implicitly] to the DateTimeAxis. Optimization: assign the axis directly to the series, don't bother adding them to the Chart.Axes property.

Thank you @jstreet and @FilipSkakun for taking the time to look at this, I appreciate your attention and patience.

-John

JohnKoz
  • 908
  • 11
  • 21