Highcharts学习笔记-line(内存使用监视)

本文介绍了如何使用highCharts库在Web页面上实时显示计算机进程的内存使用情况,包括引入所需JS文件、计算内存数据、POST请求获取数据、HTML布局及高德图表展示步骤。

1、引入highCharts的js

<script type="text/javascript" src="${resource(dir: 'js', file: 'jquery-1.6.js')}"></script>
  <script type="text/javascript" src="${resource(dir: 'js', file: 'highcharts/highcharts.js')}"></script>
  <script type="text/javascript" src="${resource(dir: 'js', file: 'highcharts/modules/exporting.js')}"></script>

 2、通过MainController中的方法计算mem

package hf

import grails.converters.JSON

class MainController {

  def calc = {
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec("tasklist /fo csv");
    int i = 0;
    def list = [];
    process.inputStream.eachLine {String line ->
      if (i >= 1) {
        String[] p = line.split(/","/)
        Mem mem = new Mem();
        mem.name = p[0].trim().replaceAll("\"","");
        mem.pid = p[1].trim() as int;
        mem.memSize = (p[4].replaceAll("\"","").replaceAll(",","").replaceAll(" ","").replaceAll("K","")) as int;
        println "name:"+mem.name+";pid:"+mem.pid+";memSize:"+mem.memSize;
        def map = [:];
        map.id=mem.id;
        map.pid = mem.pid;
        map.name = mem.name;
        map.memSize = mem.memSize;
        list.add(map);
      }
      i++;
    }
    return render(list as JSON);
  }
}

 3、在web页面中用$.post去取得数据

 $(document).ready(function() {
      $.post("${createLink(controller:'main',action:'calc')}", {type:'init'}, function(data) {
        //进程名
        var memNames = new Array();
        //进程使用的内存
        var memValues = new Array();
        jQuery.each(data, function(index,mem) {
          memNames[index] = mem.name;
          memValues[index] = mem.memSize/1024;
        });
        //alert(data);
        //alert(memValues)
        showData(memNames,memValues);
      });
    });

 4、html中div的设置

<div style="width:500px;height:500px;overflow:auto;">
<div id="container" style="width:6000px;height:400px;margin:0 auto;"></div>
</div>

 5、用highCharts显示数据

function showData(mNames,mValues) {
      chart = new Highcharts.Chart({
        chart: {
          renderTo: 'container',
          defaultSeriesType: 'line',
          marginRight: 130,
          marginBottom: 50
        },
        title: {
          text: '电脑性能',
          x: -20 //center
        },
        subtitle: {
          text: '我的电脑性能',
          x: -20
        },
        //X轴
        xAxis: {
          title:{
            text:'进程名'
          },
          categories: mNames
        },
        //y轴
        yAxis: {
          title: {
            text: '内存使用(MB)'
          },
          //y=0
          plotLines: [
            {
              value: 0,
              width: 2,
              color: '#000000'
            }
          ]
        },
        tooltip: {
          formatter: function() {
            return '<b>' + this.series.name + '</b><br/>' +
                    this.x + ': ' + this.y + 'M';
          }
        },
        //右边的提示
        legend: {
          layout: 'vertical',
          align: 'right',
          verticalAlign: 'top',
          x: -10,
          y: 100,
          borderWidth: 0
        },
        series: [
          {
            name: '内存使用',
            data: mValues
          }

        ]
      });
    }

 6,最终的结果


<think>我们正在使用React集成HighchartsGantt。根据引用[2]和[3],HighchartsGantt是一个适合在React中使用的甘特库。我们将按照以下步骤进行:1.安装必要的npm包:highcharts, highcharts-gantt,以及react的高阶组件包装器highcharts-react-official。2.在React组件中引入HighchartsHighchartsGantt模块。3.配置甘特选项,包括x轴的时间格式(根据引用[1]中提到的需求,设置时分秒格式)、系列数据等。 4.使用高阶组件HighchartsReact来渲染表。 注意:由于引用[1]提到x轴要表示24小时内的状态,我们需要设置x轴为datetime类型,并格式化时间显示。下面是一个具体的代码示例:步骤1:安装依赖```bashnpminstall highcharts highcharts-gantthighcharts-react-official``` 步骤2:创建React组件```jsximport Reactfrom'react'; importHighchartsfrom 'highcharts'; importHighchartsGantt from'highcharts/modules/gantt'; importHighchartsReact from'highcharts-react-official'; //初始化HighchartsGantt模块HighchartsGantt(Highcharts);const GanttChart =() =>{//配置表选项constoptions ={title: {text: '甘特示例'},xAxis: {type: 'datetime',//设置时间格式为时分秒,根据引用[1]的需求dateTimeLabelFormats: {millisecond:'%H:%M:%S.%L',second:'%H:%M:%S',minute:'%H:%M',hour:'%H:%M',day:'%e. %b',week: '%e.%b',month:'%b\'%y',year:'%Y'}},series:[{name: '任务1',data:[{id: 'task1',name: '任务1',start:Date.UTC(2023,6,1,8,0), //2023年7月1日08:00end: Date.UTC(2023,6,1,12,0)//2023年7月1日12:00}]}]};return (<div><HighchartsReacthighcharts={Highcharts}constructorType={'ganttChart'}options={options}/></div>);}; exportdefault GanttChart;``` 注意:上面的代码中,我们使用了`constructorType: 'ganttChart'`来指定创建甘特。另外,我们在xAxis中设置了dateTimeLabelFormats来格式化时间标签,以满足显示时分秒的需求。 关于x轴时间格式的设置,引用[1]中提到的问题是如何设置x轴时间格式为时分秒,我们在配置中通过`dateTimeLabelFormats`实现了这一点。 另外,根据引用[4]中的简单示例,我们只需要提供任务数据(包括开始和结束时间)即可。这里我们直接使用Highcharts的配置方式,将任务数据放在series的data中。如果项目中有更复杂的需求,比如任务依赖关系、进度条显示等,可以进一步配置Highcharts的选项。Highcharts Gantt的详细配置可以参考官方文档。 §§相关问题§§1.如何在Highcharts甘特中添加任务依赖关系(连线)?2.Highcharts甘特如何实现动态更新数据?3.如何自定义Highcharts甘特的进度条样式和颜色?</think>### 在 React 中集成 Highcharts 甘特的实现方案 #### 1. **安装依赖** ```bash npm install highcharts highcharts-react-official highcharts-gantt ``` #### 2. **基础组件实现** ```jsx // GanttChart.jsx import React from 'react'; import Highcharts from 'highcharts'; import HighchartsGantt from 'highcharts/modules/gantt'; import HighchartsReact from 'highcharts-react-official'; // 初始化甘特模块 HighchartsGantt(Highcharts); const GanttChart = () => { // 甘特配置 const options = { title: { text: '项目进度甘特' }, xAxis: { type: 'datetime', // 关键配置:设置24小时时间格式 (引用[1]) dateTimeLabelFormats: { day: '%H:%M', // 显示 时:分 hour: '%H:%M', minute: '%H:%M' } }, series: [{ name: '任务', data: [{ id: 'task1', name: '需求分析', start: Date.UTC(2023, 6, 15, 8), // 年,月(0-11),日,时 end: Date.UTC(2023, 6, 15, 12), completed: 0.7 // 进度百分比 }, { id: 'task2', name: 'UI设计', start: Date.UTC(2023, 6, 15, 10), end: Date.UTC(2023, 6, 15, 15), dependency: 'task1' // 依赖关系 }] }] }; return ( <HighchartsReact highcharts={Highcharts} constructorType={'ganttChart'} options={options} /> ); }; export default GanttChart; ``` #### 3. **关键功能说明** 1. **时间格式配置** 通过 `xAxis.dateTimeLabelFormats` 设置时分秒格式,满足24小时显示需求 (引用[1]) ```js dateTimeLabelFormats: { hour: '%H:%M:%S', // 时:分:秒 minute: '%H:%M' } ``` 2. **任务依赖关系** 使用 `dependency` 属性建立任务间关联 (引用[2]) ```js data: [{ id: 'task1', // ... }, { id: 'task2', dependency: 'task1' // 依赖task1完成 }] ``` 3. **实时数据更新** 结合 React 状态管理实现动态更新: ```jsx const [tasks, setTasks] = useState([...]); useEffect(() => { const options = { series: [{ data: tasks }] }; // 通过chartRef更新表 chartRef.current.chart.update(options); }, [tasks]); ``` #### 4. **高级功能扩展** ```jsx // 添加交互功能 (引用[2]) const options = { plotOptions: { gantt: { dragDrop: { liveRedraw: true // 启用拖拽更新 } } }, tooltip: { pointFormat: '<b>进度: {point.completed:.0%}</b>' } }; // 添加里程碑标记 data: [ ..., { name: '版本发布', milestone: true, // 里程碑标志 start: Date.UTC(2023, 6, 20) } ] ``` #### 5. **最佳实践建议** 1. **组件封装** 创建可复用的 `GanttComponent` 接受 `data` 作为 prop 2. **响应式设计** 添加自适应配置: ```js responsive: { rules: [{ condition: { maxWidth: 768 }, chartOptions: { legend: { enabled: false } } }] } ``` 3. **性能优化** 大数据量时启用 `boost` 模块: ```js import Boost from 'highcharts/modules/boost'; Boost(Highcharts); ``` ### 实现效果 最终将生成包含以下特性的甘特- 24小时内的时间刻度显示(时:分:秒) - 任务依赖关系连线 - 进度百分比可视化 - 拖拽调整功能 - 里程碑标记 [^1]: 时间格式配置参考 Highcharts 文档 [^2]: 交互功能实现基于 Highcharts Gantt 特性
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值