This is a migrated thread and some comments may be shown as answers.

Changing the labels of x-axis in a scatter line chart

11 Answers 672 Views
Chart (HTML5)
This is a migrated thread and some comments may be shown as answers.
squaretec
Top achievements
Rank 2
squaretec asked on 13 Dec 2013, 01:21 AM
Hi I have defined a programmatically chart as follows:

ASPX Part:
<telerik:RadHtmlChart ID="RadHtmlChartSerie" runat="server" Transitions="true" Skin="Office2007" >
                                                          <Appearance>
                                                              <FillStyle BackgroundColor="White"></FillStyle>
                                                          </Appearance>
                                                          <ChartTitle>
                                                              <Appearance Align="Center" BackgroundColor="White" Position="Top"></Appearance>
                                                          </ChartTitle>
                                                          <Legend>
                                                              <Appearance BackgroundColor="White" Position="Bottom"></Appearance>
                                                          </Legend>
 
                                                          <PlotArea>
                                                              <Appearance>
                                                                  <FillStyle BackgroundColor="White"></FillStyle>
                                                              </Appearance>
                                                              <XAxis DataLabelsField="Dia">
                                                                  <LabelsAppearance RotationAngle="90" />
                                                                  <TitleAppearance Text="Fecha" />
                                                                  <MajorGridLines Color="#EFEFEF" Width="1"></MajorGridLines>
                                                                  <MinorGridLines Color="#F7F7F7" Width="1"></MinorGridLines>
                                                                  <TitleAppearance Position="Center" RotationAngle="0" Text="Día"></TitleAppearance>
                                                              </XAxis>
                                                              <YAxis>
 
                                                                  <LabelsAppearance />
                                                                  <TitleAppearance Text="Valor" />
                                                                  <MajorGridLines Color="#EFEFEF" Width="1"></MajorGridLines>
                                                                  <MinorGridLines Color="#F7F7F7" Width="1"></MinorGridLines>
                                                              </YAxis>
                                                          </PlotArea>
 
 
                                                          <ChartTitle Text="Serie Analizada">
                                                          </ChartTitle>
                                                      </telerik:RadHtmlChart>

And on The server part,  add  Dynamically a scatter series to the Chart:

CS:
//Declare a ScatterSeries
       ScatterSeries pointMinimos = new ScatterSeries();
        pointMinimos.Name = "Mínimo";
        pointMinimos.Appearance.FillStyle.BackgroundColor = Color.Green;
        pointMinimos.LabelsAppearance.Visible = false;
        pointMinimos.TooltipsAppearance.DataFormatString = "{1}";
        pointMinimos.TooltipsAppearance.Color = Color.White;
 
//Fills The ScatterSeries
        foreach (DataRow dr in dsMaxMin.Tables["dtMinimos"].Rows)
        {           
          
ScatterSeriesItem itemCierre = new ScatterSeriesItem(Convert.ToDecimal(dr["X"].ToString()), Convert.ToDecimal(dr["Y"].ToString()));           
             
            pointMinimos.SeriesItems.Add(itemCierre);
 
        }
//Add The series to the Chart with XAxis numerical values
        RadHtmlChartSerie.PlotArea.Series.Add(pointMinimos);
 
//Clear the XAxis Items
      RadHtmlChartSerie.PlotArea.XAxis.Items.Clear();  
 
//Here the column "Dia" have a date string
foreach (DataRow row in dsMaxMin.Tables["dtMinimos"].Rows)
                            {
 
                                string formattedLabelText = string.Format("{0:MMMM}", row["Dia"].ToString());
                                AxisItem newAxisItem = new AxisItem(formattedLabelText);
 
                                RadHtmlChartSerie.PlotArea.XAxis.Items.Add(newAxisItem);
 
                            }

But, the labels on the X Axis dosn't changes , and have the numerical label.
 
I check the link  http://www.telerik.com/help/aspnet-ajax/htmlchart-date-axis.html but only works with databound RadHtmlChart which automatically detects the DateTime object data in the column/field and adjusts the distribution of the items on the XAxis.

There is a way to change the labels once defined the series in the server code?
Thanks A lot¡¡¡

Regards
Jorge RR

11 Answers, 1 is accepted

Sort by
0
Danail Vasilev
Telerik team
answered on 16 Dec 2013, 02:08 PM
Hi Jorge,

RadHtmlChart support two types of XAxis:
  • category axis, which uses discrete data (i.e.. categories), so that custom text can be set to the XAxis' categories/items.
  • numeric axis which uses continuous data, so that labels are rendered on equal intervals (i.e. steps) and displays the actual point from the axis. This type of axis doesn't let you use custom text. You can, however, specify  a particular format string for the labels through the DataFormatString property of the axis.

If you are trying, however, to create and add DateTime scatter series items programmatically I can suggest that you initially convert the X value to its JavaScript DateTime representation and then use the desired DateTime format for the XAxis. Such an example is illustrated in this forum thread.


Regards,
Danail Vasilev
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
David
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 17 May 2018, 01:13 PM

That does not work for me unfortunately.

 Here is my simplified code:

 <telerik:RadHtmlChart runat="server" ID="ScatterChart">                  
                    <PlotArea>
                        <XAxis Type="Auto" >
                            <LabelsAppearance RotationAngle="90" />
                        </XAxis>
                        <YAxis AxisCrossingValue="0"
                        </YAxis>            
                    </PlotArea>
                    <Legend>
                        <Appearance Position="Right">
                        </Appearance>
                    </Legend>
                </telerik:RadHtmlChart>

Dim loopCount As Integer = 1
  For Each dr As DataRow In dt.Rows
                ScatterChart.PlotArea.XAxis.Items.Add(dr("Industry"))           

                Dim mySeriesBase As Telerik.Web.UI.ScatterSeries = New Telerik.Web.UI.ScatterSeries
                mySeriesBase.Name = dr("Industry") & " - Base"
                mySeriesBase.MarkersAppearance.Size = 10
                mySeriesBase.LabelsAppearance.Visible = False

                mySeriesBase.SeriesItems.Add(New Telerik.Web.UI.ScatterSeriesItem(loopCount, dr("BaseLQ")))      

                loopCount = loopCount + 1

            Next

            ScatterChart.DataBind()

0
Marin Bratanov
Telerik team
answered on 22 May 2018, 06:43 AM
Hi David,

When using a scatter chart (i.e., ScatterSeries), the x-axis does not contain categories (items), so you cannot do this. Instead, its labels are at regular intervals (steps) according to the data range and chart settings.

So, you can use templates for its labels (that's a new feature since Danail's answer above) in order to change what they show, in case the DataFormatString does not suit your needs: https://docs.telerik.com/devtools/aspnet-ajax/controls/htmlchart/functionality/clienttemplate/using-client-templates-legend-axes#client-templates-for-axes-labels.


Regards,
Marin Bratanov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
David
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 22 May 2018, 12:48 PM

Hi Marin,

  I am familiar with ClientTemplate, however not sure how to use it in this case considering i am creating series manually and do not hook up datatable as datasource.

Please advice

Thank you

David

0
Marin Bratanov
Telerik team
answered on 22 May 2018, 02:44 PM
Hi David,

The axis label template is not tied to the series, so it is always defined in the same manner. Here's an example:

protected void Page_Init(object sender, EventArgs e)
{
    RadHtmlChart chart = new RadHtmlChart();
    chart.ID = "chart1";
    for (int i = 0; i < 2; i++)
    {
        ScatterSeries series = new ScatterSeries();
        series.Name = "series" + i;
        for (int j = 0; j < 10; j++)
        {
            series.SeriesItems.Add(new ScatterSeriesItem(i + j, j));
        }
        chart.PlotArea.Series.Add(series);
    }
    chart.PlotArea.XAxis.LabelsAppearance.ClientTemplate = "my #=value#";
    placeholder1.Controls.Add(chart);
}


Regards,
Marin Bratanov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
David
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 22 May 2018, 04:28 PM

Well, i thought it was pretty clear that i need something different than value.

In my case it should be industry name. If i would feed my chart from datatable i imagine something like "#=dataItem.Industry#" would work. But i create my chart differently. I am posting my code again just in case:

Dim loopCount As Integer = 1
  For Each dr As DataRow In dt.Rows
                ScatterChart.PlotArea.XAxis.Items.Add(dr("Industry"))           

                Dim mySeriesBase As Telerik.Web.UI.ScatterSeries = New Telerik.Web.UI.ScatterSeries
                mySeriesBase.Name = dr("Industry") & " - Base"
                mySeriesBase.MarkersAppearance.Size = 10
                mySeriesBase.LabelsAppearance.Visible = False

                mySeriesBase.SeriesItems.Add(New Telerik.Web.UI.ScatterSeriesItem(loopCount, dr("BaseLQ")))      

                loopCount = loopCount + 1

            Next

            ScatterChart.DataBind()

So, how can i add Industry to xAxis labels in my example?

0
Marin Bratanov
Telerik team
answered on 24 May 2018, 11:53 AM
Hi David,

The axis does not know about the series and their data so this is not possible. The only argument the axis label template receives is its value (text).

If you have a numerical x-axis (like when you use scatter series) the chart determines the min and max numerical values for the axis range, divides that by the current label step and renders labels with the appropriate text.

If you have a categorical x-axis (like when you use a line series) the x-axis has its own data source - set of items or a field name that it generates labels for. Since they can be strings the developer controls, you can create a hash table that has corresponding label text value from the data source and desired text, and use that for the templates. Only with such a x-axis can you add items to the x-axis, when a scatter series is used they will be ignored.

If you will be having one item per scatter series, then you should transpose the data source so you have one series with appropriate data, values and corresponding x-axis label field value.


Regards,
Marin Bratanov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
David
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 24 May 2018, 01:18 PM

Understood

Thank you

0
David
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 24 May 2018, 02:19 PM

Marin,

  So, i am trying to solve problem by adding additional chart underneath the scatter one. What is the best way to hide everything but xAxis for a chart?

0
Marin Bratanov
Telerik team
answered on 25 May 2018, 11:34 AM
Hello David,

The main part of the chart is the plot area and you can't hide it. You can hide axes as they are auxiliary in many charts, but not the main chart area.


Regards,
Marin Bratanov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
David
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 29 May 2018, 03:10 PM

            chartLinesCommodity.PlotArea.YAxis.MajorGridLines.Visible = False
            chartLinesCommodity.PlotArea.YAxis.MinorGridLines.Visible = False
            chartLinesCommodity.PlotArea.XAxis.MajorGridLines.Visible = False
            chartLinesCommodity.PlotArea.XAxis.MinorGridLines.Visible = False

Seemed to achieve most of what i needed

Thank you

Tags
Chart (HTML5)
Asked by
squaretec
Top achievements
Rank 2
Answers by
Danail Vasilev
Telerik team
David
Top achievements
Rank 1
Iron
Iron
Veteran
Marin Bratanov
Telerik team
Share this question
or