Highcharts : selection

2019-09-09 22:45发布

问题:

I made a barchart with dates. Currently, to remove the dates, you must click on the dates in the legend. I wish delete multiple dates at once. Let's take an example. My dates range from 1960 to 2015. If I would have on my chart the dates from 2000 to 2005, so I would like that if you click on 1960 and 1999, then all the dates between are deleted / hidden. It goes faster when many dates. How to do ?

Here is my code :

$(function () {
    $('#container').highcharts({
        chart: {
            type: 'column'
        },
        colors:[
            '#7cb5ec',
            '#434348',
            '#90ed7d',
            '#f7a35c',
            '#8085e9',
            '#f15c80',
            '#e4d354',
            '#2b908f',
            '#f45b5b',
            '#91e8e1',
            '#DA70D6',
            '#1E90FF',
            '#E0F000'
            ],
        legend:{
            itemStyle:{
            fontSize:'10px',
            font:'10pt',
            color:'#000000'
          }
        },
        title:{
            style:{
                fontSize:'0px'
           }
        },
        subtitle:{
            style:{
            fontSize:'0px'
          }
        },
        xAxis: {
            categories: [
                '',
            ],
        },
        yAxis: {
            min: 15,
            title: {
                text: 'Number',
                style:{
                    fontSize:'0px'
                }
            }
        },
        tooltip: {        
            shared: false,
            useHTML: true
        },
        plotOptions: {
            column: {
                pointPadding: 0.2,
                borderWidth: 0
            }
        },
        series: [{
            name: '2004',
            data: [49.9],

        }, 
        {
            name: '2005',
            data: [83.6]

        }, 
        {
            name: '2006',
            data: [48.9]

        }, 
        {
            name: '2007',
            data: [69.1]

        }, 
        {
            name: '2008',
            data: [83.6]

        }, 
        {
            name: '2009',
            data: [40.9]

        },
        {
            name: '2010',
            data: [69.9]

        }, 
        {
            name: '2011',
            data: [83]

        }, 
        {
            name: '2012',
            data: [28.9]

        },
        {
            name: '2013',
            data: [40.9]

        }, 
        {
            name: '2014',
            data: [81.6]

        }, 
        {
            name: '2015',
            data: [24.9]

        },
        {
            name: '2016',
            data: [46.4]

        }]
    });
});

Thanks

回答1:

Rather than having each year as its own series, I would recommend that you place the years as the values in your x-axis (as categories) and then use Highcharts' native zoom function to allow users to select a certain date range.

I've updated your fiddle with some changes, which I explain below: https://jsfiddle.net/brightmatrix/hr28s27L/

First, in your chart option, I added zoomType: 'x' to allow users to use their mouse to zoom in on specific years. The value x means they can only zoom horizontally (across), which makes for a "cleaner" interaction.

chart: {
  type: 'column',
  zoomType: 'x'
},

Second, I updated your x-axis with the years as the categories/values:

xAxis: {
  categories: ['2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016']
},

Lastly, I updated your series to show just the data points, which are then plotted on the y-axis:

series: [{
  name: 'data by year',
  data: [49.9,83.6,48.9,69.1,83.6,40.9,69.9,83,28.9,40.9,81.6,24.9,46.4]
}]

Here is what happens when a user clicks and drags their mouse cursor:

The shaded box is what they are selecting. Once they release the cursor, the chart will automatically adjust to show only what they chose. A "Reset Zoom" button will appear at the top right to allow them to go back to all years:

Update (July 19, 2016): I did some research on adding a simple slider element to this chart that allows users to choose from a range of years.

An updated version of my fiddle that shows this example chart with sliders can be found here: https://jsfiddle.net/brightmatrix/uvat8u05/.

The code to handle the sliders is shown below. I discovered that explicitly setting the slider values to integers using the parseInt() function solved a couple of problems:

  • You can correctly compare the slider values to make sure users can't choose an end year that is earlier than the start year.
  • The setExtremes() function correctly shows a single year when users choose the same start and end year.
  • All x-axis labels are shown at all times. Before I added parseInt(), some labels disappeared when users chose different values in the sliders.

Please be sure to read the comments I've left in both the HTML and Javascript panes, as I've included additional details on why I made certain code decisions.

// on change handler for both sliders
$('.mySlider').bind('change', function(e) {

    e.preventDefault();
    var chart = $('#container').highcharts();

    // convert the slider values to integers to make sure setExtremes() works as expected
    var slider1Val = parseInt($('input[name="slider1"]').val());
    var slider2Val = parseInt($('input[name="slider2"]').val());

    if (slider2Val < slider1Val) {
        // warn the user if they try to choose an end year that is later than the start year
        alert("You can't choose an end year that is earlier than your start year.");
    } else {
        // use setExtremes to set the x-axis ranges based on the values in the sliders
        chart.xAxis[0].setExtremes(slider1Val, slider2Val);
    }

});

I hope this information is helpful for you.



标签: highcharts