2012-07-12 37 views
12

我想知道是否有人成功实施了使用Highcharts的自适应设计,使他们的图表在移动和桌面上都看起来不错。使Highcharts.js图表​​在移动和桌面上看起来不错

默认情况下,当您调整浏览器屏幕的大小时,Highcharts会进行重新缩放,只是X轴由刻度标记文本和条形图看起来很高并且太瘦(太压缩)而混乱。 要了解我的意思,你可以去this page并调整浏览器大小。

我认为这些问题可能通过减少数据点的数量说原始数量的三分之一,虽然我想知道如何以编程方式使用Highcharts的API来完成。如果这听起来不是一个好主意,我也对其他想法或解决方案感兴趣,他们可能会想出在移动设备上使用Highcharts(或者甚至可能是不同的JS图表库,其中多设备解决方案可能更容易实现?)。

+2

http://angulartutorial.blogspot.in/2014/03/responsive-highchart.html – Prashobh 2014-03-26 08:37:28

+0

参考这一点,看看是否有帮助 - http://jsfiddle.net/Behseini/qheh4w0n/ – 2016-04-06 07:36:50

回答

17

该解决方案看起来相当简单。

只是不给图表的固定宽度,即不定义宽度或设置width:100%,而且,与我提到的演示不同,条形图宽度和伴随的条形会缩小,与浏览器宽度一样多降低。

+0

为什么这个答案downvoted? – 2013-08-16 12:28:42

4

这可能取决于您显示的图表类型。在移动设备上,如果您显示的是柱状图,则可能需要旋转图表以使其成为条形图。

如果您显示折线图,则可以“对数据进行”范围确定,以便仅显示获取点所需的最少点数。在放大时,重新确定数据的范围以适应当前视图。这可以通过使用一些事件与一些手动js结合来完成。

+0

它不太可能是一个解决方案,事实上这是一个重点。你必须告诉计算机@TimPeterson你想做什么,不要让它只为你猜。 – jcolebrand 2012-07-12 19:56:02

0

为例与自举

<!DOCTYPE html> 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta charset="utf-8" /> 
    <title>Highcharts data from JSON Response</title> 
     <style> 
     body{ 
      margin-top: 30px; 
      margin-left:40px; 
     } 
     .col-md-4{ 
     padding-left:5px !important; 
     padding-right:5px !important; 
     } 
     </style> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
    <script src="http://code.highcharts.com/highcharts.js"></script>  
<script src="https://code.highcharts.com/modules/exporting.js"></script> 
    <script type="text/javascript"> 
     // Data gathered from http://populationpyramid.net/germany/2015/ 

// Age categories 
var categories = ['0-4', '5-9', '10-14', '15-19', 
     '20-24', '25-29', '30-34', '35-39', '40-44', 
     '45-49', '50-54', '55-59', '60-64', '65-69', 
     '70-74', '75-79', '80-84', '85-89', '90-94', 
     '95-99', '100 + ']; 
$(document).ready(function() { 
    Highcharts.chart('container', { 
     chart: { 
      type: 'bar' 
     }, 

     xAxis: [{ 
      categories: categories, 
      reversed: false, 
      labels: { 
       step: 1 
      } 
     }, { // mirror axis on right side 
      opposite: true, 
      reversed: false, 
      categories: categories, 
      linkedTo: 0, 
      labels: { 
       step: 1 
      } 
     }], 
     yAxis: { 
      title: { 
       text: null 
      }, 
      labels: { 
       formatter: function() { 
        return Math.abs(this.value) + '%'; 
       } 
      } 
     }, 

     plotOptions: { 
      series: { 
       stacking: 'normal' 
      } 
     }, 

     tooltip: { 
      formatter: function() { 
       return '<b>' + this.series.name + ', age ' + this.point.category + '</b><br/>' + 
        'Population: ' + Highcharts.numberFormat(Math.abs(this.point.y), 0); 
      } 
     }, 

     series: [{ 
      name: 'Male', 
      data: [-2.2, -2.2, -2.3, -2.5, -2.7, -3.1, -3.2, 
       -3.0, -3.2, -4.3, -4.4, -3.6, -3.1, -2.4, 
       -2.5, -2.3, -1.2, -0.6, -0.2, -0.0, -0.0] 
     }, { 
      name: 'Female', 
      data: [2.1, 2.0, 2.2, 2.4, 2.6, 3.0, 3.1, 2.9, 
       3.1, 4.1, 4.3, 3.6, 3.4, 2.6, 2.9, 2.9, 
       1.8, 1.2, 0.6, 0.1, 0.0] 
     }] 
    }); 
    Highcharts.chart('container2', { 
     chart: { 
      type: 'bar' 
     }, 
     title: { 
      text: 'Population pyramid for Germany, 2015' 
     }, 
     subtitle: { 
      text: 'Source: <a href="http://populationpyramid.net/germany/2015/">Population Pyramids of the World from 1950 to 2100</a>' 
     }, 
     xAxis: [{ 
      categories: categories, 
      reversed: false, 
      labels: { 
       step: 1 
      } 
     }, { // mirror axis on right side 
      opposite: true, 
      reversed: false, 
      categories: categories, 
      linkedTo: 0, 
      labels: { 
       step: 1 
      } 
     }], 
     yAxis: { 
      title: { 
       text: null 
      }, 
      labels: { 
       formatter: function() { 
        return Math.abs(this.value) + '%'; 
       } 
      } 
     }, 

     plotOptions: { 
      series: { 
       stacking: 'normal' 
      } 
     }, 

     tooltip: { 
      formatter: function() { 
       return '<b>' + this.series.name + ', age ' + this.point.category + '</b><br/>' + 
        'Population: ' + Highcharts.numberFormat(Math.abs(this.point.y), 0); 
      } 
     }, 

     series: [{ 
      name: 'Male', 
      data: [-2.2, -2.2, -2.3, -2.5, -2.7, -3.1, -3.2, 
       -3.0, -3.2, -4.3, -4.4, -3.6, -3.1, -2.4, 
       -2.5, -2.3, -1.2, -0.6, -0.2, -0.0, -0.0] 
     }, { 
      name: 'Female', 
      data: [2.1, 2.0, 2.2, 2.4, 2.6, 3.0, 3.1, 2.9, 
       3.1, 4.1, 4.3, 3.6, 3.4, 2.6, 2.9, 2.9, 
       1.8, 1.2, 0.6, 0.1, 0.0] 
     }] 
    }); 
    Highcharts.chart('container3', { 
     chart: { 
      type: 'bar' 
     }, 
     title: { 
      text: 'Population pyramid for Germany, 2015' 
     }, 
     subtitle: { 
      text: 'Source: <a href="http://populationpyramid.net/germany/2015/">Population Pyramids of the World from 1950 to 2100</a>' 
     }, 
     xAxis: [{ 
      categories: categories, 
      reversed: false, 
      labels: { 
       step: 1 
      } 
     }, { // mirror axis on right side 
      opposite: true, 
      reversed: false, 
      categories: categories, 
      linkedTo: 0, 
      labels: { 
       step: 1 
      } 
     }], 
     yAxis: { 
      title: { 
       text: null 
      }, 
      labels: { 
       formatter: function() { 
        return Math.abs(this.value) + '%'; 
       } 
      } 
     }, 

     plotOptions: { 
      series: { 
       stacking: 'normal' 
      } 
     }, 

     tooltip: { 
      formatter: function() { 
       return '<b>' + this.series.name + ', age ' + this.point.category + '</b><br/>' + 
        'Population: ' + Highcharts.numberFormat(Math.abs(this.point.y), 0); 
      } 
     }, 

     series: [{ 
      name: 'Male', 
      data: [-2.2, -2.2, -2.3, -2.5, -2.7, -3.1, -3.2, 
       -3.0, -3.2, -4.3, -4.4, -3.6, -3.1, -2.4, 
       -2.5, -2.3, -1.2, -0.6, -0.2, -0.0, -0.0] 
     }, { 
      name: 'Female', 
      data: [2.1, 2.0, 2.2, 2.4, 2.6, 3.0, 3.1, 2.9, 
       3.1, 4.1, 4.3, 3.6, 3.4, 2.6, 2.9, 2.9, 
       1.8, 1.2, 0.6, 0.1, 0.0] 
     }] 
    }); 
}) 

    </script> 
</head> 
<body> 
<div class="col-md-12 row"> 
<div class="col-md-4"> 
hello 
</div> 
    <div class="col-md-8 row"> 
    <div class="col-md-4"><div id="container" style="height: 400px;border:1px solid;"></div></div> <div class="col-md-4"><div id="container2" style="height: 400px;border:1px solid;"></div></div> 
    <div class="col-md-4"><div id="container3" style="height: 400px;border:1px solid;"></div></div> 
    </div> 
    </div> 


</body> 
</html> 
1

可以设定图表div容器width:100%。 然后,只需删除高图宽度属性。我解决它为一个sparkline图表。现在它是移动响应。

Highcharts.chart('my-sparkline-chart, { 
     chart: { 
      type: 'areaspline', 
      height: '70', 
      //width: '189', //comment width property. 
      spacing: [0, 0, 0, 0], 
      backgroundColor: "transparent" 
     } 
... 
相关问题