📄 chartfactory.html
字号:
<FONT color="green">256</FONT> * For instance if <code>percentDiffForMaxScale</code> is 10 (10%), a <a name="line.256"></a><FONT color="green">257</FONT> * difference of 5% will have a half shade of red/green, a difference of <a name="line.257"></a><FONT color="green">258</FONT> * 10% or more will have a maximum shade/brightness of red/green.<a name="line.258"></a><FONT color="green">259</FONT> * <P><a name="line.259"></a><FONT color="green">260</FONT> * The chart object returned by this method uses a {@link PiePlot} instance<a name="line.260"></a><FONT color="green">261</FONT> * as the plot.<a name="line.261"></a><FONT color="green">262</FONT> * <p><a name="line.262"></a><FONT color="green">263</FONT> * Written by <a href="mailto:opensource@objectlab.co.uk">Benoit <a name="line.263"></a><FONT color="green">264</FONT> * Xhenseval</a>.<a name="line.264"></a><FONT color="green">265</FONT> *<a name="line.265"></a><FONT color="green">266</FONT> * @param title the chart title (<code>null</code> permitted).<a name="line.266"></a><FONT color="green">267</FONT> * @param dataset the dataset for the chart (<code>null</code> permitted).<a name="line.267"></a><FONT color="green">268</FONT> * @param previousDataset the dataset for the last run, this will be used <a name="line.268"></a><FONT color="green">269</FONT> * to compare each key in the dataset<a name="line.269"></a><FONT color="green">270</FONT> * @param percentDiffForMaxScale scale goes from bright red/green to black,<a name="line.270"></a><FONT color="green">271</FONT> * percentDiffForMaxScale indicate the change <a name="line.271"></a><FONT color="green">272</FONT> * required to reach top scale.<a name="line.272"></a><FONT color="green">273</FONT> * @param greenForIncrease an increase since previousDataset will be <a name="line.273"></a><FONT color="green">274</FONT> * displayed in green (decrease red) if true.<a name="line.274"></a><FONT color="green">275</FONT> * @param legend a flag specifying whether or not a legend is required.<a name="line.275"></a><FONT color="green">276</FONT> * @param tooltips configure chart to generate tool tips?<a name="line.276"></a><FONT color="green">277</FONT> * @param urls configure chart to generate URLs?<a name="line.277"></a><FONT color="green">278</FONT> * @param subTitle displays a subtitle with colour scheme if true<a name="line.278"></a><FONT color="green">279</FONT> * @param showDifference create a new dataset that will show the % <a name="line.279"></a><FONT color="green">280</FONT> * difference between the two datasets. <a name="line.280"></a><FONT color="green">281</FONT> *<a name="line.281"></a><FONT color="green">282</FONT> * @return A pie chart.<a name="line.282"></a><FONT color="green">283</FONT> */<a name="line.283"></a><FONT color="green">284</FONT> public static JFreeChart createPieChart(String title,<a name="line.284"></a><FONT color="green">285</FONT> PieDataset dataset,<a name="line.285"></a><FONT color="green">286</FONT> PieDataset previousDataset,<a name="line.286"></a><FONT color="green">287</FONT> int percentDiffForMaxScale,<a name="line.287"></a><FONT color="green">288</FONT> boolean greenForIncrease,<a name="line.288"></a><FONT color="green">289</FONT> boolean legend,<a name="line.289"></a><FONT color="green">290</FONT> boolean tooltips,<a name="line.290"></a><FONT color="green">291</FONT> boolean urls,<a name="line.291"></a><FONT color="green">292</FONT> boolean subTitle,<a name="line.292"></a><FONT color="green">293</FONT> boolean showDifference) {<a name="line.293"></a><FONT color="green">294</FONT> <a name="line.294"></a><FONT color="green">295</FONT> PiePlot plot = new PiePlot(dataset);<a name="line.295"></a><FONT color="green">296</FONT> plot.setLabelGenerator(new StandardPieSectionLabelGenerator());<a name="line.296"></a><FONT color="green">297</FONT> plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));<a name="line.297"></a><FONT color="green">298</FONT> <a name="line.298"></a><FONT color="green">299</FONT> if (tooltips) {<a name="line.299"></a><FONT color="green">300</FONT> plot.setToolTipGenerator(new StandardPieToolTipGenerator(<a name="line.300"></a><FONT color="green">301</FONT> StandardPieToolTipGenerator.DEFAULT_SECTION_LABEL_FORMAT));<a name="line.301"></a><FONT color="green">302</FONT> }<a name="line.302"></a><FONT color="green">303</FONT> if (urls) {<a name="line.303"></a><FONT color="green">304</FONT> plot.setURLGenerator(new StandardPieURLGenerator());<a name="line.304"></a><FONT color="green">305</FONT> }<a name="line.305"></a><FONT color="green">306</FONT> <a name="line.306"></a><FONT color="green">307</FONT> List keys = dataset.getKeys();<a name="line.307"></a><FONT color="green">308</FONT> DefaultPieDataset series = null;<a name="line.308"></a><FONT color="green">309</FONT> if (showDifference) {<a name="line.309"></a><FONT color="green">310</FONT> series = new DefaultPieDataset();<a name="line.310"></a><FONT color="green">311</FONT> }<a name="line.311"></a><FONT color="green">312</FONT> <a name="line.312"></a><FONT color="green">313</FONT> double colorPerPercent = 255.0 / percentDiffForMaxScale;<a name="line.313"></a><FONT color="green">314</FONT> for (Iterator it = keys.iterator(); it.hasNext();) {<a name="line.314"></a><FONT color="green">315</FONT> Comparable key = (Comparable) it.next();<a name="line.315"></a><FONT color="green">316</FONT> Number newValue = dataset.getValue(key);<a name="line.316"></a><FONT color="green">317</FONT> Number oldValue = previousDataset.getValue(key);<a name="line.317"></a><FONT color="green">318</FONT> int section = dataset.getIndex(key);<a name="line.318"></a><FONT color="green">319</FONT> <a name="line.319"></a><FONT color="green">320</FONT> if (oldValue == null) {<a name="line.320"></a><FONT color="green">321</FONT> if (greenForIncrease) {<a name="line.321"></a><FONT color="green">322</FONT> plot.setSectionPaint(section, Color.green);<a name="line.322"></a><FONT color="green">323</FONT> } <a name="line.323"></a><FONT color="green">324</FONT> else {<a name="line.324"></a><FONT color="green">325</FONT> plot.setSectionPaint(section, Color.red);<a name="line.325"></a><FONT color="green">326</FONT> }<a name="line.326"></a><FONT color="green">327</FONT> if (showDifference) {<a name="line.327"></a><FONT color="green">328</FONT> series.setValue(key + " (+100%)", newValue);<a name="line.328"></a><FONT color="green">329</FONT> }<a name="line.329"></a><FONT color="green">330</FONT> }<a name="line.330"></a><FONT color="green">331</FONT> else {<a name="line.331"></a><FONT color="green">332</FONT> double percentChange = (newValue.doubleValue() <a name="line.332"></a><FONT color="green">333</FONT> / oldValue.doubleValue() - 1.0) * 100.0;<a name="line.333"></a><FONT color="green">334</FONT> double shade<a name="line.334"></a><FONT color="green">335</FONT> = (Math.abs(percentChange) >= percentDiffForMaxScale ? 255<a name="line.335"></a><FONT color="green">336</FONT> : Math.abs(percentChange) * colorPerPercent);<a name="line.336"></a><FONT color="green">337</FONT> if (greenForIncrease <a name="line.337"></a><FONT color="green">338</FONT> && newValue.doubleValue() > oldValue.doubleValue()<a name="line.338"></a><FONT color="green">339</FONT> || !greenForIncrease && newValue.doubleValue() <a name="line.339"></a><FONT color="green">340</FONT> < oldValue.doubleValue()) {<a name="line.340"></a><FONT color="green">341</FONT> plot.setSectionPaint(section, new Color(0, (int) shade, 0));<a name="line.341"></a><FONT color="green">342</FONT> }<a name="line.342"></a><FONT color="green">343</FONT> else {<a name="line.343"></a><FONT color="green">344</FONT> plot.setSectionPaint(section, new Color((int) shade, 0, 0));<a name="line.344"></a><FONT color="green">345</FONT> }<a name="line.345"></a><FONT color="green">346</FONT> if (showDifference) {<a name="line.346"></a><FONT color="green">347</FONT> series.setValue(key + " (" + (percentChange >= 0 ? "+" : "") <a name="line.347"></a><FONT color="green">348</FONT> + NumberFormat.getPercentInstance().format(<a name="line.348"></a><FONT color="green">349</FONT> percentChange / 100.0) + ")", newValue);<a name="line.349"></a><FONT color="green">350</FONT> }<a name="line.350"></a><FONT color="green">351</FONT> }<a name="line.351"></a><FONT color="green">352</FONT> }<a name="line.352"></a><FONT color="green">353</FONT> <a name="line.353"></a><FONT color="green">354</FONT> if (showDifference) {<a name="line.354"></a><FONT color="green">355</FONT> plot.setDataset(series);<a name="line.355"></a><FONT color="green">356</FONT> }<a name="line.356"></a><FONT color="green">357</FONT> <a name="line.357"></a><FONT color="green">358</FONT> JFreeChart chart = new JFreeChart(<a name="line.358"></a><FONT color="green">359</FONT> title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend<a name="line.359"></a><FONT color="green">360</FONT> );<a name="line.360"></a><FONT color="green">361</FONT> <a name="line.361"></a><FONT color="green">362</FONT> if (subTitle) {<a name="line.362"></a><FONT color="green">363</FONT> TextTitle subtitle = null;<a name="line.363"></a><FONT color="green">364</FONT> subtitle = new TextTitle("Bright " + (greenForIncrease ? "red" <a name="line.364"></a><FONT color="green">365</FONT> : "green") + "=change >=-" + percentDiffForMaxScale <a name="line.365"></a><FONT color="green">366</FONT> + "%, Bright " + (!greenForIncrease ? "red" : "green") <a name="line.366"></a><FONT color="green">367</FONT> + "=change >=+" + percentDiffForMaxScale + "%", <a name="line.367"></a><FONT color="green">368</FONT> new Font("SansSerif", Font.PLAIN, 10));<a name="line.368"></a><FONT color="green">369</FONT> chart.addSubtitle(subtitle);<a name="line.369"></a><FONT color="green">370</FONT> }<a name="line.370"></a><FONT color="green">371</FONT> <a name="line.371"></a><FONT color="green">372</FONT> return chart;<a name="line.372"></a><FONT color="green">373</FONT> }<a name="line.373"></a><FONT color="green">374</FONT> <a name="line.374"></a><FONT color="green">375</FONT> /**<a name="line.375"></a><FONT color="green">376</FONT> * Creates a ring chart with default settings.<a name="line.376"></a><FONT color="green">377</FONT> * <P><a name="line.377"></a><FONT color="green">378</FONT> * The chart object returned by this method uses a {@link RingPlot} <a name="line.378"></a><FONT color="green">379</FONT> * instance as the plot.<a name="line.379"></a><FONT color="green">380</FONT> *<a name="line.380"></a><FONT color="green">381</FONT> * @param title the chart title (<code>null</code> permitted).<a name="line.381"></a><FONT color="green">382</FONT> * @param dataset the dataset for the chart (<code>null</code> permitted).<a name="line.382"></a><FONT color="green">383</FONT> * @param legend a flag specifying whether or not a legend is required.<a name="line.383"></a><FONT color="green">384</FONT> * @param tooltips configure chart to generate tool tips?<a name="line.384"></a>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -