i'm triying to paint a geographical chart using the info from this answer: https://stackoverflow.com/a/26868280/518 to "colore" an SVG graphic and from this answer: https://stackoverflow.com/a/11436655/518 to transform it in a BufferedImage. I have done it but every time i want to refresh the chart i have reload the SVG.
My question is: It's posible to not reload the SVG file every time i want to refresh the chart?
I paste my working code, i think that commenting the first line and uncommenting the node-replacing code may work, but instead it produce an unaltered BufferedImage
protected void refreshImage(List<GeographicChartData>geoDataList)throws Exception{
loadWorldMap(); // reads the SVG file into svgDoc property
SVGStyleElement newColorNode=(SVGStyleElement)svgDoc.createElementNS(SVGConstants.SVG_NAMESPACE_URI, "style");
newColorNode.setAttributeNS(null, "type", "text/css");
if(geoDataList!=null && geoDataList.size()>0){
for(GeographicChartData geoData:geoDataList){
double ppto=geoData.getPptoAmount();
if(ppto!=0){
Color pctColor=pct2Color.interpolateColor((geoData.getRealAmount()-ppto)/ppto);
String colorString="."+geoData.getCountryIso()+" {fill: #"+getColorString(pctColor)+";}";
newColorNode.appendChild(svgDoc.createCDATASection(colorString));
}
}
}
/*****
* I think that this has to replace the color node and refresh the BufferedImage produced, but it doesn't.
styleNode is a property setted on loadWorldMap Method
*/
/*if(oldColorNode==null){
styleNode.getParentNode().appendChild(newColorNode);
}else{
styleNode.getParentNode().replaceChild(newColorNode, oldColorNode);
}
oldColorNode=newColorNode;*/
styleNode.getParentNode().appendChild(newColorNode); // the code above don't work, so i need this
TranscoderInput transcoderInput = new TranscoderInput(svgDoc);
ImageTranscoder imageTranscoder=new BufferedImageTranscoder();
imageTranscoder.transcode(transcoderInput, null);
labelMap.setIcon(new ImageIcon(worldMapImage[0]));
}
This is the code of BufferedImageTranscoder:
class BufferedImageTranscoder extends ImageTranscoder{
public BufferedImage createImage(int w, int h) {
return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
}
public void writeImage(BufferedImage image, TranscoderOutput out) throws TranscoderException {
worldMapImage[0] = image;
}
}