热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

设置一个要显示在图表js上的数组-SetanarraytobedisplayedatChartjs

Forexampleihavethearray[1,2,3,4,5,6,7,8,9,10,11,12,13]andionlywanttodisplaythesubarray

For example i have the array [1,2,3,4,5,6,7,8,9,10,11,12,13] and i only want to display the subarray [5,6,7,8,9].

例如,我有一个数组[1、2、3、4、5、6、7、8、9、10、11、12、13],我只想显示子数组[5、6、7、8、9]。

Is this posible using the Chart.js library?

这是用图表表示的可能性吗?js库?

EDIT: Fiirst of all i am showing the complete array on the chart. After clicking a button, i will have the subarray displayed. Any ideas of to do so?

编辑:首先,我要在图表上显示完整的数组。单击按钮后,将显示子数组。有这样做的想法吗?

1 个解决方案

#1


0  

The easiest way would be to just destroy the chart (using the chart variable) and construct a new chart using the new data.

最简单的方法是销毁图表(使用图表变量),并使用新的数据构造一个新的图表。

For instance, if you already have constructed it using

例如,如果您已经使用它构造了它

...
var ctx = document.getElementById("chart").getContext("2d");
var myChart = new Chart(ctx).Bar(data);

You need to destroy it first using

你需要先毁掉它

myChart.destroy();

and then make the new chart

然后制作新的图表

myChart = new Chart(ctx).Bar(newData);

where newData is the new data object.

newData是新的数据对象。

You could also update the old data object (if you are not using it for anything else) instead of using a new object, like so

您还可以更新旧的数据对象(如果您不将其用于任何其他用途),而不是使用新的对象,如so

data.labels = [5, 6, 7, 8, 9]; 
data.datasets[0].data = [5, 6, 7, 8, 9]; 
myChart.destroy();
myChart = new Chart(ctx).Bar(data);

Fiddle - http://jsfiddle.net/5u3ahg7L/

小提琴——http://jsfiddle.net/5u3ahg7L/

(the chart updates with the new data after a 2 second delay, you don't need the setTimeout wrapper - it's just for demonstration)

(图表在延迟2秒后更新新数据,您不需要setTimeout包装器——它只是为了演示)


You could also do this using the prototype methods .update() and .removeData() (http://www.chartjs.org/docs/#line-chart-prototype-methods for Line chart methods - each type has the similar methods) but since your changes require you to remove data from both ends of the graph, .destroy() would be an easier option.

您还可以使用prototype方法.update()和. removedata () (http://www.chartjs.org/docs/# Line -chart-prototype-methods for Line chart方法——每种类型都有类似的方法)来实现这一点。


推荐阅读
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文介绍了如何利用JavaScript或jQuery来判断网页中的文本框是否处于焦点状态,以及如何检测鼠标是否悬停在指定的HTML元素上。 ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
author-avatar
革启兵
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有