Home Forums Chart Support Labels not working correctly

Labels not working correctly

Viewing 9 posts - 1 through 9 (of 9 total)
  • #21792

    Hi

    I’m trying to create a gender comparison chart. After som help from the CanvasJS team I got it to look almost like I want it but the labels are incorrect.

    Heres the code

    var dataSet = [{ x: 10, y: 71, position: "upperValue", label: "13-17" },
                   { x: 20, y: 55, position: "upperValue", label: "18-24" },
                   { x: 30, y: 55, position: "upperValue", label: "24-34" },
                   { x: 40, y: 65, position: "upperValue", label: "35-44" },
                   { x: 50, y: 95, position: "upperValue", label: "45-54" },
                   { x: 10, y: 51, position: "lowerValue", label: "13-17" },
                   { x: 20, y: 95, position: "lowerValue", label: "18-24" },
                   { x: 30, y: 20, position: "lowerValue", label: "25-34" },
                   { x: 40, y: 85, position: "lowerValue", label: "35-44" },
                   { x: 50, y: 15, position: "lowerValue", label: "45-54" }];
    
       var chartGender = new CanvasJS.Chart("chartGender",{
         axisY: {
           labelFormatter: function(e) {
             if (e.value < 0)
               return label = -1 * e.value;
             else
               return label =  e.value;
           }
         },
         toolTip:{
           contentFormatter: function ( e ) {
             if( e.entries[0].dataPoint.y < 0)
               return e.entries[0].dataPoint.x + ": " + -1 * e.entries[0].dataPoint.y ;
             else
               return e.entries[0].dataPoint.x + ": " + e.entries[0].dataPoint.y ;
           }},
         data: [
           {
             type: "column",
             dataPoints: [ ]
           }
         ]
       });
    
       for( var i = 0; i < dataSet.length; i++ )
         if( dataSet[i].position === "lowerValue")
           chartGender.options.data[0].dataPoints.push({ x:dataSet[i].x, y: (-1 * dataSet[i].y), color: "#3C6399"});
         else
           chartGender.options.data[0].dataPoints.push({ x:dataSet[i].x, y: (dataSet[i].y), color: "#899BC0" });
    
      chartGender.render();

    This will end up with labels like 10,20,30,40,50 and not the labels I’d like to have: 13-17, 18-24, 25-34 etc.

    #21804

    Samuel,

    While adding the dataPoints to chart, you need to add labels along with x and y values. Also setting axisX interval according to the dataPoints should work fine in your scenario. Please take a look at this jsfiddle.

    __
    Priyanka M S
    Team CanvasJS

    #21808

    Looks great! Thx.

    How do I add “Women” as dark blue (name of dark blue dataset) and “Men” as the lighter blue with this setup?

    #21814

    Samuel,

    You can use indexLabel for the same. Please take a look at this jsfiddle.

    __
    Priyanka M S
    Team CanvasJS

    #21816

    Hi

    Thx but that was not what I meant.

    I don’t want a label on each bar. I want 1 label in total per color, below the chart. Like this one: https://canvasjs.com/javascript-charts/multi-series-chart/

    The labels “Myrtle Beach” “Martha Vineyard” etc.

    #21840

    @Samuel
    Hey,

    You can use the legend to do what you are asking.
    Look at https://canvasjs.com/docs/charts/chart-options/legend/ for more info about it.
    Hopefully this helps.

    #21845

    Samuel,

    Legends are used to represent dataSeries in a chart. You can’t have two legends for one dataSeries. With a workaround, you can display span elements as inactive legends. Please take a look at this updated jsfiddle.

    __
    Priyanka M S
    Team CanvasJS

    #21992

    I still have some problems adding more values to the chart. It will only show 5 but I have 7.

    var dataSet = [{ x: 10, y: 71, position: "upperValue", label: "13-17" },
                   { x: 20, y: 55, position: "upperValue", label: "18-24" },
                   { x: 30, y: 55, position: "upperValue", label: "25-34" },
                   { x: 40, y: 65, position: "upperValue", label: "35-44" },
                   { x: 50, y: 95, position: "upperValue", label: "45-54" },
                   { x: 50, y: 95, position: "upperValue", label: "55-64" },
                   { x: 40, y: 65, position: "upperValue", label: "65" },
                   { x: 10, y: 51, position: "lowerValue", label: "13-17" },
                   { x: 20, y: 95, position: "lowerValue", label: "18-24" },
                   { x: 30, y: 20, position: "lowerValue", label: "25-34" },
                   { x: 40, y: 85, position: "lowerValue", label: "35-44" },
                   { x: 40, y: 85, position: "lowerValue", label: "45-54" },
                   { x: 40, y: 85, position: "lowerValue", label: "55-64" },
                   { x: 50, y: 15, position: "lowerValue", label: "65" }];
    
    var chartGender = new CanvasJS.Chart("chartGender",{
      animationEnabled: true,
      theme: "light2",
      axisX: {
        interval: 10,
      },
      axisY: {
        labelFormatter: function(e) {
          if (e.value < 0)
            return label = -1 * e.value;
          else
            return label =  e.value;
        }
      },
      toolTip:{
        contentFormatter: function ( e ) {
          if( e.entries[0].dataPoint.y < 0)
            return e.entries[0].dataPoint.x + ": " + -1 * e.entries[0].dataPoint.y ;
          else
            return e.entries[0].dataPoint.x + ": " + e.entries[0].dataPoint.y ;
        }},
      data: [
        {
          type: "column",
          dataPoints: [ ]
        }
      ]
    });
    
    for( var i = 0; i < dataSet.length; i++ )
      if( dataSet[i].position === "lowerValue")
        chartGender.options.data[0].dataPoints.push({ x:dataSet[i].x, y: (-1 * dataSet[i].y), label: dataSet[i].label, color: "#229DA0"});
      else
        chartGender.options.data[0].dataPoints.push({ x:dataSet[i].x, y: (dataSet[i].y), label: dataSet[i].label, color: "#27B6BA" });
    
      chartGender.render();
    

    The chart must be able to show more than 5 age groups.

    #22003

    Samuel,

    The fiddle which we had provided before works for any number of dataPoints, provided that the x values of “upperValue” sync with the x values of “lowerValue”. Please take a look at this jsfiddle.

    __
    Priyanka M S
    Team CanvasJS

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.