JFreechart เองไม่สามารถสร้างกราฟิก SVG ได้ แต่คุณสามารถใช้สิ่งอื่นเพื่อช่วยสร้างได้
หนึ่ง: Java สร้างแผนที่เค้ก SVG ซึ่งมาพร้อมกับฉลากเพื่อแสดงส่วนของตัวแทนสีแต่ละสี
แพ็คเกจ com.tellhow.svg; นำเข้า Java.file; "#f2e692", "#aa1111", "#799ae1", "#3e941b", "#66cc00", "#297110", "#d6a97b", "#82522b", "#aaaaff", "#1111aa" " #ff2222", "#ffaaaa"}; 'utf-8'?> "); sfile.append ("/n "); sfile.append (<svg xmlns: svg = 'http: //www.org/2000/svg'" ("/n"); = 'http://www.w3.org/1999/xlink' "); sfile.append ("/n "); sfile.append (" xml: space = 'default' "); sfile. ต่อท้าย ("/// n "); sfile.append (" เวอร์ชัน = '1.1' width = '100%' ความสูง = '100%' viewbox = '0 0 2024 570'> "); sfile.append ("/n "); <"<" <"<defs> </defs>"); , 0,0,16.384, -9.83) 'xmlns =' http://www.w3.org/2000/svg '> "); sfile.append ("/n "); // cyclic สร้างแท็กเส้นทาง String path = creatpath (502, 300, 300, percents, ชื่อ); ); sfile.append ("</svg>"); Percents เปอร์เซ็นต์อาร์เรย์* @param ชื่อแสดงชื่อของตัวแทนสี* @return*/ Stric Stric String creatpath (double x0, double y0, double [] percents, string [] ชื่อ) {Stringbuffer sfile = new Stringbuffer (); x1 = 0; Double Radian = 0; ++) == 0) {radian = getradian (percents [0]); middlex = (x0+getcos (textradian*r); middley = (y0-getsin (textradian)*r); สองเปอร์เซ็นต์ = math.round (percents [0]*100) /100.0;. k = math.abs (( midey-y0)/(midex-x0)); 50; 0) {เมื่อประเภทคือ 0 แผนที่เค้กแสดงผลถ้า (textradian <(math.pi/2))) {sfile.append ("<line x1 = '"+middlex+"' y1 = '" middley+"' x2 = '"+(middlex+math.cos (sita)*linelen)+"' y2 = '"+(math.sin (sita)*linelen))+"' stroke = '#000000'/> "); sfile.append ("/n"); TextLen))+"'space =' อนุรักษ์ 'font-family =' tahoma 'font-size =' 21 'fill =' stroke = 'eline สีแดง-shift =' baseline '>"+เปอร์เซ็นต์+"%</text>" );} อื่นถ้า (textradian> (math.pi/2) && textradian <math.pi) {sfile.append ("<line x1 = '"+middlex+"' y1 = '"+middley+"' x2 = '" +(middlex-math.cos (sita)*ผ้าลินิน)+"'y2 ='"+(math.sin (sita)*linlen))+"'stroke ='#000000 '/>"); "/n"); "'space =' preserve 'font-family =' tahoma 'font-size =' 21 'fill =' red 'strike =' red 'baseline-shift =' baseline '>"+เปอร์เซ็นต์+"%</text> ");} อื่นถ้า (textradian> (math.pi) && textradian <(math.pi*3/2)) sfile.append (" <line x1 = '"+middlex+"' y1 = '"middley+"' x2 = '"+(middlex-math.cos (sita)*linelen)+"' y2 = '"+(midgey+(math.sin (sita)*linlen)+" stroke ='#000000 '/> ") ; sfile.append ("/n"); *textLen)+"'space =' รักษา 'font-font-font-size =' 21 'fill =' red 'stroke =' red 'baseline-shift =' baseline '>"+เปอร์เซ็นต์+"%</text>" . .COS (SITA)*linelen)+"'y2 ='"+(Middley+Math.sin (SITA)*linelen)+"'stroke ='#000000 '/>"); ); 'รักษา' font-family = 'tahoma' font-size = '21 'fill =' red 'stroke =' red 'baseline-shift =' baseline '> "+เปอร์เซ็นต์+"%</text> "");} sfile .append ("/n") (getradian (percents [0])> math.pi) {sfile.append ("<path d = 'm"+x0+""+y0+"l"+(x0+r) +"+r+" a "+r+" "+r+" 0 1 0 "+x1+" "+y1+" l "+x0+"+y0+"z 'fill ='"+สี [0]+"/>" );} else {sfile.append ("<path d = 'm"+x0+""+y0+"l"+(x0+r)+"+r+" a "+r+" "r+"+x1+"" +y1+"l"+x0+""+y0+"z 'fill ='"+สี [0]+"'/>");} sfile.append ("/n"); +2*r)+"'y ='"+(y0-r/2.0+n)+"'width = '60' ความสูง = '30' fill = '"+color [i]+"' stroke = ' #fffffff 'stroke-dasharray =' 1,1 ' /> "); sfile.append (" /n "); 2*r+80)+"' y = '"+(y0-r /2.0+n+ 25)+"'space =' อนุรักษ์ 'font-family =' font-size = '28' fill = '"+colours [0]+"' stroke = '#000000' stroke-dasharray = '1,1' -Shift = 'baseline'> "+ชื่อ [0]+" </text> "); ("/n ");} else {textradian = radian+(getradian (percents [i])/2); ส่วนโค้งของเส้นบ่งชี้และ x -axis ); ); ::::::::::::::::::::::: กระทาน ::::::::::::::::::::::: กระทาน ::::::::::: MATH.ATAN (K); ) {sfile.append ("" <line x1 = '"+midex+"' y1 = '"+middley+"' x2 = '"+(middlex+math.cos (sita)*linelen)+" y2 =' " +(math.sin (sita)*linelen)+"'stroke ='#000000 '/>"); math.cos (sita)*textlen)+"'y ='"+(math.sin (sita (sita)*textlen))+"'space =' preserve 'font-font-font-size =' 21 ' = 'red' stroke = 'red' baseline-shift = 'baseline'> "+เปอร์เซ็นต์+"%</text> "); {sfile.append (" <line x1 = '"+middlex+"' y1 = '" middley+"'x2 ='"+(middlex-math.cos (sita)*linelen)+"'y2 ='"+(middey- (math.sin (sita)*linelen)+"stroke = '#000000' /> "); sfile.append ("/n "); sfile.append (" <text x = '"+(middlex-math.cos (sita)*textlen)+"' y = '"+(คณิตศาสตร์ sin (sita)*textlen)+"'space =' รักษา 'font-family =' tahoma 'font- size = '21' fill = 'red' stroke = 'Red' baseline-Shift = 'baseline'>"+เปอร์เซ็นต์++ "%</text>"); adian <(math.pi*3/2))))))) {sfile.append ("<line x1 = '"+middlex+"' y1 = '"+middley+" 'x2 =' "+(middlex-math.cos (sita)*linelen)+" 'y2 =' "+(mthdley+(math.sin (sita)*linlen))+" 'strong ='#000000 '/> "); sfile.append ("/n ");" <text x = '"+(midex-math.cos (sita)*textlen)+"' y = '"+(math.sin (sita)*textlen )+"'space =' preserve 'font -family =' tahoma 'font-size = '21' fill = 'red' stroke = 'red' baseline-shift = 'baseline'>"+เปอร์เซ็นต์+"%</text> "); radian> (math.pi*3/2) && textradian <(math.pi*2)) {sfile.append (" <line x1 = '"+middlex+"' y1 = '"middley+"' x2 = '"+(middlex+math.cos (sita)*linelen)+"' y2 = '"+(middley+math.sin (sita)*linelen)+" stroke ='#000000 '/> "); sfile.append ("" " /n"); )*textlen))+"'space =' รักษา 'font-family =' tahoma 'font-size =' 21 'fill =' red 'stroke =' red 'baseline-shift =' baseline '>+เปอร์เซ็นต์+"% " % </xt> "");} sfile.append ("/n"); สำคัญกว่า) ถ้า (getRadian (percents [i])> math.pi) {sfile.append ("<path d = 'm"+x0+""+y0+"l"+x1+""+y1+"a"+r+ ""+r+"0"+(x1 = x0+getcos (เรเดียน)*r)+"+(y1 = y0-getsin (เรเดียน)*r)+" l "+x0+" "+y0+" z 'fill = '"+colors [i]+"'/> "" "" "" "); +y1+"+r+" "+r+" 0 0 "+(x1 = (x1 = x0+getcos (เรเดียน)*r)+"+(y1 = y0-getsin (เรเดียน)*r)+"l"+x0+ ""+y0+"z 'fill ='"+color [i]+"' /");} sfile.append (" /n");} n+= 50; (x0+2*r)+"'y ='"+(y0 (y0 -r /2.0+n)+"'Width = '60' ความสูง = '30 'fill =' "+colors [i]+" 'stroke ='#fffffff 'dasharray =' 1,1 ' /> "" ") ;;" /n "); sfile.append (" <text x =' "+(x0+2*r+80)+ "'y ='"+(y0-r/2.0+n+25)+"'space =' รักษา 'font-family =' zongyi 'font-size = '28' fill = '"+สี [0]+" 'stroke ='#000000 'stroke-dasharray =' 1,1 :::::::::::::::::::::::::::::::: กระทาน ::::::::::::::::::::::: กระทาน ::::::::::::::::::::::::::::::::::::: กระทาน. > "+ชื่อ [i]+" </text> "); sfile.append ("/n ");}} ส่งคืน sfile.toStration ();} // ส่งคืนอาร์คสาธารณะคงที่ getradian (สอง fenshu) { return (fenshu*math.pi)/50;} // กลับไปที่ Sine Public Static Double Getsin (Double Radian) {return math.sin (radian);} // กลับไปที่ Yu Xian สาธารณะคง Radian) {return math.cos (radian); ข้อมูล [0]+"One", "Dispeller:"+data [1]+"", "line:"+data [2]+"", "+data [3]+" 个 "," 母线: " +ข้อมูล [4]+"个"}; ข้อมูล), ชื่อ);} catch (Exception e) {E.printstackTrace ();}} ส่วนตัวคงที่ [] getpercent (int data []) {double sum = 0; ความยาว]; สำหรับ (int i = 0; i <data.length; i ++) {sum+= data [i]; ] = (data [i]/sum)*100;} return percents;} โมฆะสาธารณะคงที่ createsvg (สตริง filerealealpath, double [] percents, สตริง [] ชื่อ) tion {string sfile = เริ่มต้น (percents, ชื่อ); Byte [] bytefil = sfile.getBytes ("UTF-8"); ไฟล์ svgfile = ไฟล์ใหม่ (filereRealAth); ; fos.write (bytefil);2. Java สร้างแผนที่เค้ก SVG 3D
(สิ่งนี้สามารถสร้างกราฟิก แต่ไม่สมบูรณ์ฉันไม่ได้แก้ไขรหัสอีกต่อไปเพราะฉันคิดว่าสิ่งนี้ไม่คุ้มค่าและ JFreechart อาจจะดีกว่าฟังก์ชั่นนั้นแข็งแกร่งขึ้น แต่โปรแกรมเหล่านี้ทำให้ฉันรู้ SVG มากขึ้น มีป้ายกำกับบางส่วน) 3d.) ผลของเอฟเฟกต์ที่สร้างขึ้นจะมีเอฟเฟกต์ความผิดปกติ ปรับ: SVGDeveloper
com.xj.svg; นำเข้า Java.io.file; #9aabee ","#3e941b ","#f2e692 ","#66cc00 ","#297110 ","#d6a97b ","#82522b "," #aaaaff ","#1111aa ","#ff2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222 #ffaaaa ","#aa1111 "}; โมฆะคงที่สาธารณะหลัก (สตริง [] args) {ข้อมูลสองเท่า [] = {20,20,50}; ลอง {createsvg (" f: /f.svg ", getpercent (ข้อมูล ; StringBuffer (); 'http://www.w3.org/2000/svg' "); sfile.append ("/n "); sfile.append (" xmlns = 'http://www.w3.org/ 2000/svg' "); sfile.append ("/n "); sfile.append (" xmlns: xlink = 'http: //www.org/1999/xlink' "); n"); = 'default' "); sfile.append ("/n "); sfile.append (" เวอร์ชัน = '1.1' width = 'ความสูง =' 100% 'viewbox =' 0 0 1024 600 '> "); sfile ผนวก ("/n"); (เส้นทาง); * @param x0 Origin x * @param y0 Origin y * @param langr * @param shortr * @param fenshu * @return * / สตริงคงที่ c reatepath (สองเท่า x0, คู่ y0, double langr, double shortr, double percents [] {StringBuffer SFILE = New StringBuffer (); stroke = '#000000' transform = 'matrix (1,0,0,1.638, -9.83)' xmlns = 'http://www.w3.org/2000/svg'> "); n "); สำหรับ (int i = 0; i <percents.length; i ++) {system.out.println (" i: "+i); radian = radian+getradian (percents [i]); // แฟนตัวแรก -รูปตัวหนึ่งกับแฟนตัวแรก, ผลรวมของอาร์ค +เรเดียน); ถ้า (เรดเดียน == math.pi/2) {xbottom = x0; 3/2) {xbottom = x0; Shortr+Tanradian*Tanradian*langr*langr; // 底面的 x 坐标 ybottom = y0- (tanradian*langr*shortr) /math.sqrt (sqvalue); // 底面的 y 坐标} อื่นถ้า (เรเดียน> คณิตศาสตร์. pi/2 && radian <= math.pi) {System.out.println ("if2:"+radian); > math.pi && radian <math.pi* 3/2) {system.out.println ("if3:"+radian); (Tanradian*langr*shortr) /math.sqrt (sqvalue);} อื่นถ้า (radian> math.pi*3/2 && radian <math.pi*2) {systemm .out.println ("if4:"+radian ); xbottom = x0+(langr*shortr) /mth.sqrt (sqvalue) .pi {// มากกว่า pi arc นั่นคือเปอร์เซ็นต์มากกว่า 50% sfile.append ("<gry = '" สี [i]+"'>"); "); sfile.append (" <path d = 'm "+x0+" "+y0+" l "+(x0+langr)+" "+y0+"+langr+"" shortr+"0 1 0"+xbottom+" "+ybottom+" z ' /> "); sfile.append (" /n "); sfile.append (" <path d =' m "+(x0+langr)+" "+(y0-50)+" "+langr+" "+shortr+" 0 1 0 "+xbottom+" "+(ybottom-50)+" l "+xbottom+"+ybottom+"a"+langr+"" shortr+"0 1 1"+(x0++ langr)+""+y0+"z ' />"); "+(x0+langr)+" "+(y0-50)+" a "+langr+"+shortr+"0 1 0"+xbottom+"+(ybottom-50)+" z '/> "); sfile ผนวก ("/n"); ]+"'>"); sfile.append ("/n"); +y0+"a"+langr+""+shortr+"0 0 0"+xbottom+""+ybottom+"z ' />"); x0+langr)+""+(y0-50)+"a"+langr+""+shortr+"0 0 0"+xbottom+"+(ybottom-50)+" l "+xbottom+" "ybottom+" a " +langr+""+shortr+"0 0 1"+(x0+langr)+"" y0+"z ' />"); m "+x0+" ""+(y0-50)+"l"+(x0+langr)+"+(y0-50)+" a "+langr+" ""+shortr+"0 0 0"+xbottom+" "+(ybottom-50)+" z '/> "); sfile.append ("/n "); sfile.append (" </g> "); sfile. ต่อไป ("/n ");}} else {ถ้า (radian == math.pi/2) {xbottom1 = x0; /2) {xbottom1 = x0; // 底面的 x 坐标 ybottom1 = y0+shortr; // 底面的 y 坐标} อื่น {double tanradian = math.abs (math.tan (radian)); *Tanradian*langr*langr; ybottom1 = y0- (tanradian*langr*shortr) /math.sqrt (sqvalue); /math.sqrt (sqvalue); ; {system.out.println ("i" f4: "+radian); xbottom1 = x0+(langr*shortr) /math.sqrt (sqvalue); ybottom1 = y0+(tanradian*langr*shortr) /math.sqrt ;}} tradian (percents [i]) >> math.pi) {// มากกว่า pi arc นั่นคือเปอร์เซ็นต์มากกว่า 50% system.out.println ("มากกว่า pi"); ("<gry = '"+สี [i]+"'>"); +xbottom+""+ybottom+"+langr+" "" "+shortr+" 0 1 0 "+xbottom1+" "+ybottom1+" z ' /> "); sfile.append (" /n "); sfile.append (" < PATH D = 'M "+(XBOTTOM)+" "" "" "" ")+" A "+langr+" "+shortr+" 0 1 0 "+xbottom1+"+(ybottom1-50)+"l"+ xbottom1+""+ybottom1+"+langr+" "" "" "0 1 1"+xbottom+""+ybottom+"z ' />"); = 'm "+x0+" "+(y0-50)+" l "+(xbottom)+"+(ybottom-50)+"a"+langr+""+shortr+"0 1 0"+xbottom1+"" (ybottom1-50)+"z '/>"); println ("น้อยกว่า pi"); g fill = '"+สี [i]+"'> "); sfile.append ("/n "); sfile.append (" <path d = 'm " ""+y0+"l"+xbottom+""+ybottom+"a"+langr+""+shortr+"0 0 0"+xbottom1+"" ybottom1+"z ' />"); ; +ybottom1+"a"+langr+""+shortr+"0 0 1"+xbottom+""+ybottom+"z ' />"); 50)+"l"+(xbottom)+""+(ybottom-50)+"a"+langr+"+shortr+" 0 0 0 "+xbottom1+"+(ybottom1-50)+"z '/>") sfile.append ("/n"); ;} // กลับไปที่อาร์คสาธารณะคงที่สองเท่าสองเท่า (สองเปอร์เซ็นต์) {return (เปอร์เซ็นต์*math.pi)/50;} // กลับไปที่ Sine State State Double Getsin (Double Radian) {return math.sin (radian);} // กลับไปที่สตริงสาธารณะคงที่ double getcos (สองเรเดียน) {return math.cos (radian);} ส่วนตัวคงที่ double [] getpercent (Dou data []) {double sum = 0; [] = ใหม่ double [data.length]; สำหรับ (int i = 0; i <data.length; i ++) {sum+= data [i]; i ++) {percents [i] = (data [i]/sum)*100;} return percents;} public static void createsvg (String filerealalpath, double [] percents) {string sfile = เริ่มต้น Byte [] bytefil = sfile.getBytes ("UTF-8"); ไฟล์ svgfile = ไฟล์ใหม่ (filererealaph); fos.write (bytefil);3. ใช้ JFreeChart เพื่อสร้างกราฟิก SVG แบบไดนามิก:
นำเข้า Java.Awt.Rectangle; CategoryDataset; */ คลาสสาธารณะ Barchartdemo {โมฆะคงที่สาธารณะหลัก (สตริง [] args) พ่น IOException {หมวดหมู่ข้อมูลชุดข้อมูล = getDataSet (); "แผนภูมิผลไม้", // ชื่อแผนภูมิ "ผลไม้", // แสดงฉลาก "ผลผลิต", // แท็กแสดงผลของชุดข้อมูลเพลาตัวเลข, // ชุดข้อมูล plotorientation.vertical, // ทิศทางแผนภูมิ: แนวนอน , // ว่าจะแสดงตำนาน (สำหรับไดอะแกรมคอลัมน์ง่าย ๆ จะต้องเป็นเท็จ) เท็จ // ว่าจะสร้างเครื่องมือเท็จ // ว่าลิงก์ URL ถูกสร้างขึ้น); (0,0,400,300); ExportChartASSVG (แผนภูมิ, ขอบเขต, FO_SVG); รูปภาพ jfreechart chart = chartfactory.createBarchart3d ("Outlet", // แท็กแสดง "เอาต์พุต", // ชุดข้อมูลฉลากแสดง, // ชุดข้อมูล plotorientation.vertical, // ทิศทางแผนภูมิ: แนวนอนไม่ว่าจะสร้างลิงก์ URL); jpg, 1, แผนภูมิ, 400, 300, null);} ในที่สุด {ลอง {fos_jpg.close ();} catch (Exception e) {}} / ** * รับวัตถุชุดข้อมูลง่ายๆสำหรับการสาธิต * * @return * / Private StaticDatasetAset () {) {) YDATASET DATASET = ใหม่ DefaultCategoryDatAset (); (200, "1", "Lychee"); "," lychee "); ชุดข้อมูลส่งคืน;} / *** รับชุดข้อมูลชุดค่าผสมที่ใช้โดยการสาธิต * * * @return * / หมวดหมู่คงที่ส่วนตัว DataSetAsetDatAset2 () {DefaultCategoryDataSetAset = ใหม่เริ่มต้น (100, "ปักกิ่ง", "Apple"); , "ลูกแพร์"); "); ชุดข้อมูล AddValue (200," Guangzhou "," Pear "); Dataset.addvalue (300," Beijing "," องุ่น "); องุ่น "); ชุดข้อมูล. addValue (300," กวางโจว "," องุ่น "); ชุดข้อมูล. addvalue (400," ปักกิ่ง "," กล้วย "); ชุดข้อมูล. addvalue (400," เซี่ยงไฮ้ "," กล้วย "); "," Banana "); DataSet.addValue (500," Beijing "," Litchi "); DataSet.addvalue (500," Shanghai "," Litchi ");"); Jfreechart ไปยังไฟล์ SVG แผนภูมิ, ขอบเขตสี่เหลี่ยมผืนผ้า, ไฟล์ svgfile) พ่น IOEException สร้างเอกสารการ domimimplementation XML = domimplemplementation; / วาดแผนภูมิในแผนภูมิ SVG Generator ;เมื่อใช้สิ่งนี้คุณต้องใส่ใจกับสองคะแนน:
1. JFreechart เองไม่สามารถสร้างกราฟิก SVG ได้คุณต้องใช้ Batik
Batik-Awt-util.jar
Batik-dom.jar
Batik-svggen.jar
Batik-util.jar
Batik-xml.jar
jfreechart -.0.0.jar
2. เป็นไปได้ที่จะสร้าง SVG อีกคนหนึ่งเรียกว่าผู้เล่น Renesis SVG ผู้ชมจะสนับสนุนภาษาจีน
เนื้อหาข้างต้นเป็นเนื้อหาทั้งหมดของ Java Cakes SVG และ JFreechart สร้างแผนภูมิ SVG