Search code examples
c#arrayshighchartsdotnethighcharts

DotNetHighcharts - How to only use first column of 2d array as Data


I've got a 2d array of objects that I am passing into my seriesSet for creating a column graph, one column is a percentage, which is what I want to see plotted on the graph where the other I only want to show in the tooltip.

Can any of you highcharts geniuses think of a way I can do this? e.g. it looks something like this

{ 100, 20 }
{ 100, 20 }
{ 80 , 16 }
{ 80 , 16 }
{ 40 , 8  }
{ 40 , 8  }
{ 20 , 4  }
  ...

I know now how to refer to each in the SetToolTip Formatter using 'this.x/this.y/this.point.x' which is progress since I was stuck for a quite a while trying to do that. But now I only want the first set (100, 80, 40, 20) to be used to draw the graph where the second set should only be used for the tooltip.

Coming up on 2 weeks puzzling over this work item so any help is appreciated

edit: to clarify, the graph draws as I want it to when I pass in a 1D object array consisting of the percentages only, then breaks when I include the second set (the counts)

so I am setting my seriesData like so:

List<DotNet.Highcharts.Options.Series> seriesSet = new List<DotNet.Highcharts.Options.Series>();
seriesSet.Add(new DotNet.Highcharts.Options.Series
{

    Type = chartType,               
    Name = "ExampleArray",
    Data = new DotNet.Highcharts.Helpers.Data(ExampleArray),
    Color = tarColor,
    PlotOptionsColumn = new DotNet.Highcharts.Options.PlotOptionsColumn
    {
        PointPadding = -0.1
    }, 
});

where example array is composed of the list of numbers above.


Solution

  • I'm not sure I understood correctly but if what you want is get a collection of all the first items in this collection and then another one for the second then:

    // Remove any item that doesn't have at least these two wanted values
    items = items.Where(item => item.Count >= 1); 
    
    // Select the sub-collection you need
    var percentages = items.Select(item => item[0]);
    var tooltips = items.Select(item => item[1]);
    

    From looking around a bit (like here) it seems you should pass a Object[] to the Data so replace ExampleArray with:

    items.Select(item => (object)item[0]);
    

        seriesSet.Add(new DotNet.Highcharts.Options.Series
        {
    
            Type = chartType,
            //Name = "Targeted_" + Convert.ToString(tarCount.Count()),                
            Name = "Targeted",
            Data = nnew DotNet.Highcharts.Helpers.Data((object[])targeted.Cast<object[]>()
                                                 .Select(item => (object)item[0])),
            Color = tarColor,
            PlotOptionsColumn = new DotNet.Highcharts.Options.PlotOptionsColumn
            {
                PointPadding = -0.1
            }, 
    
        });