Home forums Using CanvasJS Labels not working correctly

Tagged: 

This topic contains 6 replies, has 3 voices, and was last updated by  Priyanka M S 4 days, 17 hours ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #21792

    Samuel
    Member

    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

    Priyanka M S
    Member

    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

    Samuel
    Member

    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

    Priyanka M S
    Member

    Samuel,

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

    __
    Priyanka M S
    Team CanvasJS

    #21816

    Samuel
    Member

    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

    Vulpe
    Member

    @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

    Priyanka M S
    Member

    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

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

You must be logged in to reply to this topic.