mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-14 20:00:09 +00:00
[feature][API] add SaveAsImage()
This commit is contained in:
@@ -58,6 +58,7 @@
|
|||||||
|
|
||||||
## master
|
## master
|
||||||
|
|
||||||
|
* (2022.09.22) 增加`SaveAsImage()`接口保存图表到图片
|
||||||
* (2022.09.21) 修复`InsertSerie()`接口不刷新图表的问题
|
* (2022.09.21) 修复`InsertSerie()`接口不刷新图表的问题
|
||||||
* (2022.09.21) 优化`PolarChart`对`Line`热力图的支持
|
* (2022.09.21) 优化`PolarChart`对`Line`热力图的支持
|
||||||
* (2022.09.20) 增加`PolarChart`对`Heatmap`热力图的支持
|
* (2022.09.20) 增加`PolarChart`对`Heatmap`热力图的支持
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ namespace XCharts.Editor
|
|||||||
public static readonly GUIContent btnAddComponent = new GUIContent("Add Main Component", "");
|
public static readonly GUIContent btnAddComponent = new GUIContent("Add Main Component", "");
|
||||||
public static readonly GUIContent btnCovertXYAxis = new GUIContent("Covert XY Axis", "");
|
public static readonly GUIContent btnCovertXYAxis = new GUIContent("Covert XY Axis", "");
|
||||||
public static readonly GUIContent btnRebuildChartObject = new GUIContent("Rebuild Chart Object", "");
|
public static readonly GUIContent btnRebuildChartObject = new GUIContent("Rebuild Chart Object", "");
|
||||||
|
public static readonly GUIContent btnSaveAsImage = new GUIContent("Save As Image", "");
|
||||||
public static readonly GUIContent btnCheckWarning = new GUIContent("Check Warning", "");
|
public static readonly GUIContent btnCheckWarning = new GUIContent("Check Warning", "");
|
||||||
public static readonly GUIContent btnHideWarning = new GUIContent("Hide Warning", "");
|
public static readonly GUIContent btnHideWarning = new GUIContent("Hide Warning", "");
|
||||||
}
|
}
|
||||||
@@ -279,6 +280,10 @@ namespace XCharts.Editor
|
|||||||
{
|
{
|
||||||
m_Chart.RebuildChartObject();
|
m_Chart.RebuildChartObject();
|
||||||
}
|
}
|
||||||
|
if (GUILayout.Button(Styles.btnSaveAsImage))
|
||||||
|
{
|
||||||
|
m_Chart.SaveAsImage();
|
||||||
|
}
|
||||||
if (m_CheckWarning)
|
if (m_CheckWarning)
|
||||||
{
|
{
|
||||||
EditorGUILayout.BeginHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
||||||
|
|||||||
8
Plugins.meta
Normal file
8
Plugins.meta
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 6558d1464a47441d18df18c4a403b2f2
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
24
Plugins/Download.jslib
Normal file
24
Plugins/Download.jslib
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
mergeInto(LibraryManager.library, {
|
||||||
|
Download: function (str, fn) {
|
||||||
|
var msg = UTF8ToString(str);
|
||||||
|
var fname = UTF8ToString(fn);
|
||||||
|
function fixBinary(bin) {
|
||||||
|
var length = bin.length;
|
||||||
|
var buf = new ArrayBuffer(length);
|
||||||
|
var arr = new Uint8Array(buf);
|
||||||
|
for (var i = 0; i < length; i++) {
|
||||||
|
arr[i] = bin.charCodeAt(i);
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
var binary = fixBinary(atob(msg));
|
||||||
|
var data = new Blob([binary]);
|
||||||
|
var link = document.createElement('a');
|
||||||
|
link.download = fname;
|
||||||
|
link.href = URL.createObjectURL(data);
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
34
Plugins/Download.jslib.meta
Normal file
34
Plugins/Download.jslib.meta
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 821b9cd60f13648a396c76481da2191c
|
||||||
|
PluginImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
iconMap: {}
|
||||||
|
executionOrder: {}
|
||||||
|
isPreloaded: 0
|
||||||
|
isOverridable: 0
|
||||||
|
platformData:
|
||||||
|
- first:
|
||||||
|
Any:
|
||||||
|
second:
|
||||||
|
enabled: 0
|
||||||
|
settings: {}
|
||||||
|
- first:
|
||||||
|
Editor: Editor
|
||||||
|
second:
|
||||||
|
enabled: 0
|
||||||
|
settings:
|
||||||
|
DefaultValueInitialized: true
|
||||||
|
- first:
|
||||||
|
Facebook: WebGL
|
||||||
|
second:
|
||||||
|
enabled: 1
|
||||||
|
settings: {}
|
||||||
|
- first:
|
||||||
|
WebGL: WebGL
|
||||||
|
second:
|
||||||
|
enabled: 1
|
||||||
|
settings: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -588,5 +588,15 @@ namespace XCharts.Runtime
|
|||||||
SerieHelper.GetItemColor(out color, out toColor, serie, null, m_Theme);
|
SerieHelper.GetItemColor(out color, out toColor, serie, null, m_Theme);
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 保存图表为图片。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="imageType">type of image: png, jpg, exr</param>
|
||||||
|
/// <param name="savePath">save path</param>
|
||||||
|
public void SaveAsImage(string imageType = "png", string savePath = "")
|
||||||
|
{
|
||||||
|
StartCoroutine(SaveAsImageSync(imageType, savePath));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@@ -741,5 +742,11 @@ namespace XCharts.Runtime
|
|||||||
InitComponentHandlers();
|
InitComponentHandlers();
|
||||||
InitSerieHandlers();
|
InitSerieHandlers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IEnumerator SaveAsImageSync(string imageType, string path)
|
||||||
|
{
|
||||||
|
yield return new WaitForEndOfFrame();
|
||||||
|
ChartHelper.SaveAsImage(rectTransform, canvas, imageType, path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@@ -896,5 +897,62 @@ namespace XCharts.Runtime
|
|||||||
if (valueOrRate >= -maxRate && valueOrRate <= maxRate) return valueOrRate * total;
|
if (valueOrRate >= -maxRate && valueOrRate <= maxRate) return valueOrRate * total;
|
||||||
else return valueOrRate;
|
else return valueOrRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[DllImport("__Internal")]
|
||||||
|
private static extern void Download(string base64str, string fileName);
|
||||||
|
|
||||||
|
public static Texture2D SaveAsImage(RectTransform rectTransform, Canvas canvas, string imageType = "png", string path = "")
|
||||||
|
{
|
||||||
|
var cam = canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : canvas.worldCamera;
|
||||||
|
var pos = RectTransformUtility.WorldToScreenPoint(cam, rectTransform.position);
|
||||||
|
var width = rectTransform.rect.width * canvas.scaleFactor;
|
||||||
|
var height = rectTransform.rect.height * canvas.scaleFactor;
|
||||||
|
var posX = pos.x + rectTransform.rect.xMin * canvas.scaleFactor;
|
||||||
|
var posY = pos.y + rectTransform.rect.yMin * canvas.scaleFactor;
|
||||||
|
var rect = new Rect(posX, posY, width, height);
|
||||||
|
var tex = new Texture2D((int) width, (int) height, TextureFormat.RGBA32, false);
|
||||||
|
tex.ReadPixels(rect, 0, 0);
|
||||||
|
tex.Apply();
|
||||||
|
byte[] bytes;
|
||||||
|
switch (imageType)
|
||||||
|
{
|
||||||
|
case "png":
|
||||||
|
bytes = tex.EncodeToPNG();
|
||||||
|
break;
|
||||||
|
case "jpg":
|
||||||
|
bytes = tex.EncodeToJPG();
|
||||||
|
break;
|
||||||
|
case "exr":
|
||||||
|
bytes = tex.EncodeToEXR();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Debug.LogError("SaveAsImage ERROR: not support image type:" + imageType);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
var fileName = rectTransform.name + "." + imageType;
|
||||||
|
#if UNITY_WEBGL
|
||||||
|
string base64str = Convert.ToBase64String(bytes);
|
||||||
|
Download(base64str, fileName);
|
||||||
|
Debug.Log("SaveAsImage: download by brower:" + fileName);
|
||||||
|
return tex;
|
||||||
|
#endif
|
||||||
|
if (string.IsNullOrEmpty(path))
|
||||||
|
{
|
||||||
|
var dir = Application.persistentDataPath + "/SavedImage";
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
dir = Application.dataPath + "/../SavedImage";
|
||||||
|
#else
|
||||||
|
dir = Application.persistentDataPath + "/SavedImage";
|
||||||
|
#endif
|
||||||
|
if (!System.IO.Directory.Exists(dir))
|
||||||
|
{
|
||||||
|
System.IO.Directory.CreateDirectory(dir);
|
||||||
|
}
|
||||||
|
path = dir + "/" + fileName;
|
||||||
|
}
|
||||||
|
System.IO.File.WriteAllBytes(path, bytes);
|
||||||
|
Debug.Log("SaveAsImage:" + path);
|
||||||
|
return tex;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user