在同一html页面中使用不同数据复制图表/图形(Duplicate charts/graphics with different data in the same html page)
这是在网页上显示图表的HTML:
<div class="chart" id="p1"> <canvas id="c1" width="560" height="260" style="width: 560px; height: 260px;"></canvas> </div>
这就是生成图表的原因:
<script> var c1 = document.getElementById('c1'); var parent = document.getElementById('p1'); c1.width = parent.offsetWidth - 40; c1.height = parent.offsetHeight - 40; var data1 = { labels: [ 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab', 'Dom' ], datasets: [{ fillColor: 'rgba(255,255,255,.1)', strokeColor: 'rgba(255,255,255,1)', pointColor: '#123', pointStrokeColor: 'rgba(255,255,255,1)', data: [ 190, 200, 235, 390, 290, 250, 250 ] }] }; var options1 = { scaleFontColor: 'rgba(255,255,255,1)', scaleLineColor: 'rgba(255,255,255,1)', scaleGridLineColor: 'transparent', bezierCurve: false, scaleOverride: true, scaleSteps: 10, scaleStepWidth: 100, scaleStartValue: 0 }; new Chart(c1.getContext('2d')).Line(data1, options1); //@ sourceURL=pen.js </script>
我想在同一网站页面中制作具有不同值的其他图表...我已经尝试复制代码并更改id =“c1”e id =“p1”但没有...
This is the HTML that display the chart on webpage:
<div class="chart" id="p1"> <canvas id="c1" width="560" height="260" style="width: 560px; height: 260px;"></canvas> </div>
and this is the that generate the chart:
<script> var c1 = document.getElementById('c1'); var parent = document.getElementById('p1'); c1.width = parent.offsetWidth - 40; c1.height = parent.offsetHeight - 40; var data1 = { labels: [ 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sab', 'Dom' ], datasets: [{ fillColor: 'rgba(255,255,255,.1)', strokeColor: 'rgba(255,255,255,1)', pointColor: '#123', pointStrokeColor: 'rgba(255,255,255,1)', data: [ 190, 200, 235, 390, 290, 250, 250 ] }] }; var options1 = { scaleFontColor: 'rgba(255,255,255,1)', scaleLineColor: 'rgba(255,255,255,1)', scaleGridLineColor: 'transparent', bezierCurve: false, scaleOverride: true, scaleSteps: 10, scaleStepWidth: 100, scaleStartValue: 0 }; new Chart(c1.getContext('2d')).Line(data1, options1); //@ sourceURL=pen.js </script>
i want to make other charts with different values in the same website page... i already tried duplicate the codes and change id="c1" e id="p1" but nothing...
原文:https://stackoverflow.com/questions/29150278
最满意答案
有许多可能性可以避免这个问题:在评论中已经提到了延迟或撤消增量。 JVM有很多技巧,其中一些适用:
- 当
obj.doSomething(m_someVar)
是非虚方法调用(private
或final
方法,或从不重写的方法)时,它可能需要显式空检查(*),因为没有SEGV,但NPE必须在通话前被抛出。 我假设该方法没有内联,否则应该在内联后应用此分析。- 当
obj.doSomething(m_someVar)
是一个虚方法调用时,你必须做“方法调度”,即像obj.getClass().getPointerToMethod("doSomething(int)")
,以确定具体方法是什么呼叫。 我写了“类似的东西”,因为这样做非常费时并且尽可能地进行优化。 如果obj
恰好为null
,则此调度可以移动到增量之上,并且它本身将抛出NPE。- 如果你很幸运,那么调度可能看起来像
if (obj.getClass() != MyClass.class) uncommon_trap();
,这是最简单的情况(称为“单态调用站点”),但即使这涉及解除引用obj
,你再次得到一个NPE为null
。
(*)null检查可能看起来像
obj.getClass()
,它在汇编程序中是从相对于obj
指针的固定偏移量加载的单个指令。 当obj == null
,会触发未映射的页面。There are many possibilities ho to avoid the problem: Delaying or undoing the increment was already mentioned in a comment. The JVM has a big bag of tricks and some of them applies:
- When
obj.doSomething(m_someVar)
is a non-virtual method call (aprivate
orfinal
method, or a never overridden method), then it probably requires an explicit null check(*) as there would be no SEGV, but the NPE must be thrown before the call. I'm assuming the method was not inlined, as otherwise this analysis should be applied after inlining.- When
obj.doSomething(m_someVar)
is a virtual method call, then you have to do "method dispatch", i.e., something likeobj.getClass().getPointerToMethod("doSomething(int)")
in order to determine what concrete method to call. I wrote "something like" as doing this literally is very time-consuming and gets optimized away as much as possible. This dispatch can be moved above the increment and it itself will throw an NPE, ifobj
happens to benull
.- When you're lucky, then the dispatch may look like
if (obj.getClass() != MyClass.class) uncommon_trap();
, which is the simplest case (called "monomorphic call site"), but even this involves dereferencingobj
and you again get an NPE fornull
.
(*) A null check may look like
obj.getClass()
which in assembler is a single instruction loading from a fixed offset relative to theobj
pointer. Whenobj == null
then an unmapped page ist hit.
相关问答
更多-
Java:死码消除(Java: Dead code elimination)[2023-10-02]
布尔表达式的评估不应该被编译掉 - 至少通过javac。 即使Debug类文件当前对值没有任何作用,谁会说在执行时会出现这种情况? 在完成Debug类的编译后(通过使isAssertEnabled成为静态),javac仍然包含代码 - 但我期望JIT编译器将其删除(尽管您应该看到Peter引用的问题 )。 然后是否可以将该方法内容化为无,我不确定。 同样,如果JIT编译器可以做到这一点,并且如果它意识到评估参数不会有任何副作用,它可能会潜在地避免评估。 尽管如此,我不会亲自编写代码。 The evaluat ... -
我建议检查空值,而不是执行计算,而不是抛出异常。 一个例外应该是“特殊的”和罕见的,而不是一种管理控制流程的方式。 我还建议您与客户就输入参数建立合同。 如果允许空位拼出来, 如果它们不是,请清楚应该传递什么,默认值,以及如果传递空值,您承诺返回的内容。 I'd recommend checking for null and not doing the calculation rather than throwing an exception. An exception should be "excepti ...
-
没有一个完整的答案,我根本没有时间做你的问题需要但希望有用的详细的基准。 了解你的敌人 您的目标是JVM(本质上是JIT)和底层CPU /内存子系统的组合。 因此,当您进入更积极的优化时,“所有这些在JVM X上更快”在所有情况下都不可能有效。 如果您的目标市场/应用程序将大部分运行在特定的架构上,那么您应该考虑投资具体的工具。 *如果您在x86上的表现是关键因素,那么英特尔的VTune非常适合您深入描述的jit输出分析 。 * 64位和32位JIT之间的差异可能是相当大的,特别是在x86平台上,调用约定可 ...
-
没有理由这么做是不好的做法。 在您的示例中,您不清楚为什么要进行检查。 一个常见的例子可能是这样的 if (s == null || s.isEmpty()) // if s empty or not set. 要么 if (s != null && s.length() > 0) 通常,在需要时执行此操作,在这种情况下性能不重要。 通常你会写 if(x != null) 或更好 if (x == null) { // handle null, assuming this is not empt ...
-
有许多可能性可以避免这个问题:在评论中已经提到了延迟或撤消增量。 JVM有很多技巧,其中一些适用: 当obj.doSomething(m_someVar)是非虚方法调用( private或final方法,或从不重写的方法)时,它可能需要显式空检查(*),因为没有SEGV,但NPE必须在通话前被抛出。 我假设该方法没有内联,否则应该在内联后应用此分析。 当obj.doSomething(m_someVar)是一个虚方法调用时,你必须做“方法调度”,即像obj.getClass().getPointerToMe ...
-
Java null检查(Java null check)[2022-06-07]
是否有可能让thread2在NullPointerException检查后重写对象引用? 绝对地 - 它可以在play()方法执行时改变object的值,如果这就是你的意思。 这本身不会导致错误。 请注意,如果没有同步或其他内存障碍,thread2可能会更改object的值,而不会在不确定的时间段内注意到thread1。 很难说你应该做什么,没有任何关于代码更大目标的知识。 Is it possible to for thread2 to rewrite object reference after Nul ... -
检查日期范围内是否也处理空值Java的日期(Check if date between date range that also handle null values Java)[2023-10-03]
怎么样: return (startDate == null || !date.before(startDate)) && (endDate == null || !date.after(endDate)); 这使用了这两个陈述是等同的事实: !(date.before(startDate) || date.after(endDate)) !date.before(startDate) && !date.after(endDate) 而事实是|| 是一个短路,它可以防止NullPointerEx ... -
Java死去代码......这个代码有被优化掉的风险吗?(Java dead code elimination… is this code at risk of being optimized out?)[2020-01-27]
编译器不能假定getResultCount()没有副作用 - 因此它不能删除该调用。 The compiler can't assume that getResultCount() has no side-effects -- therefore it can't remove the call. -
Java null检查性能(Java null check performance)[2023-04-20]
您看到的结果可能是由于首先运行“noFnCalls”,而没有在测试和测量之前引入适当的预热。 我明白了: withFnCalls ellapsed time: 444 noFnCalls ellapsed time: 471 withFnCalls ellapsed time: 334 noFnCalls ellapsed time: 331 withFnCalls ellapsed time: 330 noFnCalls ellapsed time: 325 withFnCalls ellapsed ti ... -
Java检查null练习(Java check for null practise)[2024-02-14]
绝对不。 if (object)是原始boolean的实例,并且通过代理,包装类Boolean (自动拆箱), if (object)语法才会起作用。 另请注意, if (object)语法可用于较弱的类型化语言,如Groovy或JavaScript,并提供所有注意事项。 最后注意一个Boolean包装器在Java中也是可空的,因此if (object != null)语法实际上对于Boolean s也是有意义的(但不会为原始boolean s编译)。 Absolutely not. The if (obj ...