`

Rails flot图表 添加趋势线和拖动效果

阅读更多
Flot 是基于Jquery的图表显示插件。github上有flot的wrapper支持调用和现实图表。

这里是一些flot使用的例子
Jonathan Leto added logarithmic axis support and released it to the world here with some examples here. You can read Jonathan’s post on the flot mailing list here.  I greatly appreciate Jonathan’s contributions as log support is key to my latest project.

Flot was still missing trend lines and dragability, though.  I’ve added these features and along the way I fixed a couple of issues with the logarithmic code. I’ve posted a diff of my changes and the source code to the flot issue list and mailing list.

Examples showing dragable points and trend lines in Flot graphs follow. Flot draws plots through the html <canvas> tag. This tag is supported in Firefox 1.5+, Opera 9+, and Safari. Support for Internet Explorer is handled through the excanvas.js library. Therefore drag support in IE is slower but does function. So, in IE, drag slowly and you may have to click to release the point. I am working on fixes and improvements for this. Also, I still have an issue with a previously dragged point remaining highlighted which I need to fix. This has something to do with redrawing of the plot overlay (now fixed, nothing to do with the overlay).

Note that if selection support is enabled (to select an area on the graph, e.g. for zooming), dragging will be automatically disabled. The UI interaction of both at the same time is not desirable.

An example of a plot on a log y axis (log axes thanks to Jonathan!). The points in Series 2 are dragable. When dragged, the table below will update with the new data values.





Dragable support is off by default and must be turned on in the options object passed into the plot:

var options = {
    lines: { show: true  },
    points: {show: true, fill: true, fillcolor:'#999999' },
    selection: {mode: null},
    dragable: true,
    yaxis: { autoscaleMargin: 0.2, base: 10, tickDecimals: 1, tickSize: 1 },
    grid: { hoverable: true, clickable: true , color: "#999999"},
    legend: {show: true,
        position: 'nw',
        backgroundColor: '#ffffff',
        margin: 10,
        labelBoxBorderColor: '#999999'}
    };



Dragable support for individual lines can then be overridden by the line options:

var chart = $.plot($("#plotdiv"),
    [
    {label: "Series 1",
        dragable: false,
        clickable: false,
        hoverable: false, data: data1},
    { label: "Series 2", data: data2}
    ],
    options
);

   

When dragging, the plot fires a “plotSeriesChange” event on the placeholder div with the series index, data index, updated x and updated y values as parameters. You can bind the plot target div to this event to do something while the user is dragging the point. Note that on log graphs, the data values are no longer returned as logarithmic values, they rescaled to normal (I also updated the plotclick/hover events to rescale the values).

$("#plotdiv").bind("plotSeriesChange",
    function (event, seriesIndex, dataIndex, newx, newy) {
      tabledata[dataIndex] = [newx, newy];
      $.each(tabledata, initTable);
});



A plot with a trend line and dragable bar graph. Trend lines can be added to line or bar graphs. The trend line will dynamically recalculate for a dragable graph.





Trend lines are off by default and must be turned on in the options object passed into the plot:

var options2 = {
    trendLines: { show: true, label: "trendline"  },
    lines: { show: false  },
    points: {show: false },
    dragable: true,
    bars: {show: true, barWidth: 0.5, align: "center" },
    selection: {mode: null},
    yaxis: { autoscaleMargin: 0.2, tickDecimals: 1, tickSize: 1 },
    grid: { hoverable: true, clickable: true , color: "#999999"},
    legend: {show: true,
        position: 'nw',
        backgroundColor: '#ffffff',
        margin: 15,
        labelBoxBorderColor: '#999999'}
};



Trend line options are similar to flot line options except clickability, hoverability and dragability are forced off. You can override showing a trend line for an individual line or alter other trend line options in the individual line options:

var chart2 = $.plot($("#plotdiv2"),
    [{label: "Data", trendLines:{lineWidth: 4}, data: data3}],
    options2
);


Many Thanks to Ole for an exceptional piece of open source software and for Jonathan’s extremely useful additions!

http://www.hacknack.com/2009/02/adding-trendlines-and-dragable-to-flot/
  • 大小: 25.8 KB
  • 大小: 8.8 KB
分享到:
评论
1 楼 cheyongzi 2012-10-19  
想问一下 你这个点拖动的方法在Flot里面有吗?还是你使用的是jqPlot?不知道你还有没有这个拖动例子的代码,希望你能发给我一下 389936133@qq.com 或者我们交流一下。

相关推荐

    图表:for在Rails 5.x中用一行Ruby添加漂亮且可重复使用的图表

    由于我之前的项目涉及制作多个图表,因此我了解仅针对不同的图表在javascript和ruby中具有重复的数据结构和功能的痛苦。 图表是我的解决方案,用于将Javascript / HTML图表快速添加到导轨中。 您可以编写...

    Rails 101 入门电子书

    ### Rails 101 入门电子书知识点详解 #### 一、简介 《Rails 101 入门电子书》是一本非常适合初学者直接入门的书籍,它由xdite编写并出版于2014年6月10日。本书主要针对的是希望学习Ruby on Rails框架的读者,特别...

    Rails项目源代码

    这个“Rails项目源代码”是一个使用Rails构建的图片分享网站的完整源代码,它揭示了如何利用Rails的强大功能来创建一个允许用户上传、分享和浏览图片的应用。 1. **Rails框架基础**: Rails的核心理念是DRY(Don't...

    关于rails 3.1 cucumber-rails 1.2.0

    Rails 3.1 和 Cucumber-Rails 1.2.0 是两个在Web开发领域非常重要的工具,尤其对于Ruby on Rails框架的测试和自动化流程。本文将深入探讨这两个组件,以及它们如何协同工作来增强软件开发的效率和质量。 首先,...

    rails敏捷开发的购物车系统

    Rails 3.2.6是本文的基础框架,它是一个强大的Ruby Web应用程序框架,以其MVC(模型-视图-控制器)架构和“约定优于配置”的原则而闻名。 首先,让我们了解Rails的MVC架构。模型(Model)负责处理业务逻辑和数据...

    rails api(文档)

    英文版的Rails API文档全面且实时更新,包含了最新的特性和技术趋势。 为了开始使用Rails API,首先你需要安装Rails框架,然后创建一个特定于API的项目,使用`rails new your_api --api`命令。接着,你可以按照文档...

    rails2.3.2

    插件是 Rails 社区共享代码的一种方式,可以快速添加特定功能。 5. 测试框架:Rails 内置了测试支持,包括 Unit 测试、Functional 测试和Integration 测试,使用 RSpec 和 Test::Unit 等工具,确保代码的质量和可...

    Ruby on Rails Guides v2 - Ruby on Rails 4.2.5

    ### Ruby on Rails Guides v2 - Ruby on Rails 4.2.5 #### 一、重要概念及基础假设 - **重要概念**:本指南旨在帮助读者深入理解Ruby on Rails(以下简称Rails)4.2.5版本的核心功能与最佳实践。 - **基础假设**:...

    rails2-sample

    在这一部分,作者将引导读者如何安装和配置Ruby on Rails环境,包括Ruby语言本身、Rails框架以及相关的工具和库。这里还会涉及如何创建第一个Rails项目,以及如何运行服务器以查看项目。对于新手来说,这一步骤是至...

    使用Aptana+Rails开发Rails Web应用(中文)

    在开发Web应用时,Ruby on Rails(简称Rails)框架因其高效、简洁的代码风格和强大的社区支持而备受青睐。Aptana是一款强大的集成开发环境(IDE),尤其适用于Rails项目的开发,它提供了丰富的特性来提升开发效率。...

    Rails

    Rails由David Heinemeier Hansson在2004年创建,其设计理念是强调代码的简洁性、DRY(Don't Repeat Yourself)原则和开发效率。 Rails的核心特性包括: 1. **约定优于配置**(Convention Over Configuration):...

    Ruby on Rails安装指南(Ruby 1.8.6+Rails 2.0.2)

    Ruby on Rails 安装指南是指安装 Ruby 1.8.6 和 Rails 2.0.2 的详细步骤。首先,需要下载 Ruby One-Click Installer 版本,并安装 Ruby。然后,下载 Rails 2.0.2 版本,并安装。接下来,需要安装 Mongrel 服务器。...

    Ruby-Annotate注释Rails类模式和路由信息

    本文将深入探讨"Ruby-Annotate"工具,这是一个用于Rails项目的实用程序,能够自动为你的模型添加注释,显示有关数据库模式、关联和路由的信息。 **Ruby-Annotate** 是一个Ruby gem,它的主要功能是分析你的Rails...

    Rails进行敏捷Web开发(所有版本的源码rails3.0-4.0)

    Rails是Ruby语言的一个著名Web应用框架,以敏捷开发和“约定优于配置”为设计理念,深受开发者喜爱。在“Rails进行敏捷Web开发(所有版本的源码rails3.0-4.0)”中,包含了Rails从3.0到4.0各个主要版本的源代码,这些...

    rails 2.3.2离线安装rails 2.3.2离线安装

    rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...

    rails指南 中文版

    Ruby on Rails(简称Rails)是一个基于Ruby语言的开源Web应用框架,它遵循MVC(Model-View-Controller)架构模式,强调“约定优于配置”(Conventions over Configuration)和“Don't Repeat Yourself”(DRY,不要...

    The Rails 4 Way

    本书深入浅出地介绍了Rails 4的各种组件和功能,适合已经熟悉Ruby编程并希望深入了解Rails框架的开发人员。 #### 描述:学习ruby! 这句简短的描述暗示了本书的一个重要目标:帮助读者通过学习Rails来提高他们的...

    inspinia admin - v2.5 Rails_Full_Version

    7. **jQuery和JavaScript**:Inspinia Admin包含许多交互元素,因此熟悉JavaScript和jQuery对于实现动态效果至关重要。 8. **Asset Pipeline**:Rails的静态资源管理机制,包括CSS、JS和图片的编译、压缩和版本控制...

Global site tag (gtag.js) - Google Analytics