# Saving a configuration of a power chart in runtime?

**URL:** <https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840>\
**Category:** Ignition\
**Created:** [April 21, 2021, 2:33am UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840 "2021-04-21T02:33:00Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael2](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@michael2](https://forum.inductiveautomation.com/u/michael2)\
**Post date:** [April 21, 2021, 2:33am UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/1 "2021-04-21T02:33:00Z")

</div>

Hi everyone,

I’m using Perspective 8.1.2. I’m trying to figure out the best approach to save a configuration of the power chart in runtime (Pens and axes/plots). I’m not really sure where to start with this, so any help is appreciated. Preferably something that can be saved on the gateway and retrieved, rather than an SQL approach.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [April 21, 2021, 3:50am UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/2 "2021-04-21T03:50:11Z")

</div>

If you don’t want to go sql, then the nest best thing would be dataset tags to store the various table data. It’s not a 5min thing to setup. I would make it work for both dataset tags and sql tables, or at least make it easy to convert the code for either

---

<div class="post-metadata">

**Author:** ![michael2](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@michael2](https://forum.inductiveautomation.com/u/michael2)\
**Post date:** [April 21, 2021, 4:54am UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/3 "2021-04-21T04:54:16Z")

</div>

I think datasets are probably going to align more with my ignition skillset, can you point me to an ignition doc which has the process for turning the JSON into a dataset? I’ve not seen it done before.

---

<div class="post-metadata">

**Author:** ![sandersbud4](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sandersbud4/32/23438_2.png) [@sandersbud4](https://forum.inductiveautomation.com/u/sandersbud4)\
**Post date:** [April 21, 2021, 6:08am UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/4 "2021-04-21T06:08:54Z")

</div>

@michael2 I think you’r going on right way. I was facing same problem during last week now it’s cleared. Thanks to you for this. 🙂

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [April 21, 2021, 10:03am UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/5 "2021-04-21T10:03:55Z")

</div>

Look up jsonEncode and decode in the manual, in only on my phone atm

---

<div class="post-metadata">

**Author:** ![michael2](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@michael2](https://forum.inductiveautomation.com/u/michael2)\
**Post date:** [April 22, 2021, 12:11am UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/6 "2021-04-22T00:11:01Z")

</div>

Thanks Nick, I’ll have a look into this.

---

<div class="post-metadata">

**Author:** ![michael2](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@michael2](https://forum.inductiveautomation.com/u/michael2)\
**Post date:** [April 22, 2021, 4:41am UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/7 "2021-04-22T04:41:57Z")

</div>

Managed to get this working. Created a dataset which had a column for each JSON string in the props of the power chart (config, pens, plots, etc.), which then saved it to a tag with a name defined by the user. Does the same thing in reverse to ‘open’ the saved config.

---

<div class="post-metadata">

**Author:** ![jkramer](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@jkramer](https://forum.inductiveautomation.com/u/jkramer)\
**Post date:** [April 30, 2021, 11:20pm UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/8 "2021-04-30T23:20:14Z")

</div>

I’m looking to do the same thing, but having trouble dealing with the JSON, I think, and conversions to get it out of and back into the Power Chart properties. Can you post some simple sample code that demonstrates what you have done with this? Thanks.

---

<div class="post-metadata">

**Author:** ![michael2](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@michael2](https://forum.inductiveautomation.com/u/michael2)\
**Post date:** [May 3, 2021, 10:22pm UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/9 "2021-05-03T22:22:56Z")

</div>

There is probably a better way of doing this, but this is the way I did it:

Saving to a tag:

```auto
config = system.util.jsonEncode(self.parent.parent.getChild("PowerChart").props.config)
interaction = system.util.jsonEncode(self.parent.parent.getChild("PowerChart").props.interaction)
axes = system.util.jsonEncode(self.parent.parent.getChild("PowerChart").props.axes)
pens = system.util.jsonEncode(self.parent.parent.getChild("PowerChart").props.pens)
plots = system.util.jsonEncode(self.parent.parent.getChild("PowerChart").props.plots)
dataColumns = system.util.jsonEncode(self.parent.parent.getChild("PowerChart").props.dataColumns)
title = system.util.jsonEncode(self.parent.parent.getChild("PowerChart").props.title)
timeAxis = system.util.jsonEncode(self.parent.parent.getChild("PowerChart").props.timeAxis)
legend = system.util.jsonEncode(self.parent.parent.getChild("PowerChart").props.legend)
style = system.util.jsonEncode(self.parent.parent.getChild("PowerChart").props.style)
#put JSON Strings in dataset
headers = ['config','interaction','axes','pens','plots','dataColumns','title','timeAxis','legend','style']
rows = [[config,interaction,axes,pens,plots,dataColumns,title,timeAxis,legend,style]]
system.tag.write(FilePath, system.dataset.toDataSet(headers,rows))

```

Retrieving from a tag:

```auto
data = system.tag.read(FilePath).value
self.parent.parent.getChild("PowerChart").props.config = system.util.jsonDecode(data.getValueAt(0,0))
self.parent.parent.getChild("PowerChart").props.interaction = system.util.jsonDecode(data.getValueAt(0,1))
self.parent.parent.getChild("PowerChart").props.axes = system.util.jsonDecode(data.getValueAt(0,2))
self.parent.parent.getChild("PowerChart").props.pens = system.util.jsonDecode(data.getValueAt(0,3))
self.parent.parent.getChild("PowerChart").props.plots = system.util.jsonDecode(data.getValueAt(0,4))
self.parent.parent.getChild("PowerChart").props.dataColumns = system.util.jsonDecode(data.getValueAt(0,5))
self.parent.parent.getChild("PowerChart").props.title = system.util.jsonDecode(data.getValueAt(0,6))
self.parent.parent.getChild("PowerChart").props.timeAxis = system.util.jsonDecode(data.getValueAt(0,7))
self.parent.parent.getChild("PowerChart").props.legend = system.util.jsonDecode(data.getValueAt(0,8))
self.parent.parent.getChild("PowerChart").props.style = system.util.jsonDecode(data.getValueAt(0,9))

```

---

<div class="post-metadata">

**Author:** ![jkramer](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@jkramer](https://forum.inductiveautomation.com/u/jkramer)\
**Post date:** [May 19, 2021, 4:08pm UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/10 "2021-05-19T16:08:45Z")

</div>

Thanks for sharing that!

---

<div class="post-metadata">

**Author:** ![Chris\_Bingham](https://avatars.discourse-cdn.com/v4/letter/c/e5b9ba/32.png) [@Chris\_Bingham](https://forum.inductiveautomation.com/u/Chris_Bingham)\
**Post date:** [November 8, 2022, 7:22pm UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/11 "2022-11-08T19:22:01Z")

</div>

Perhaps an old thread, but thought I'd mention a couple of things for additional reference:

1. Ignition Exchange hosts a project ([Ad Hoc Trends](https://inductiveautomation.com/exchange/46/overview)) which offers some interesting insight as to possibilities for Saving / Retrieving chart configurations, as well as some ways to customize the chart during runtime. We opted to go a different route, in an effort to simplify a few layers.

2. Our solution for exporting, a 'Button' component on the same page as the trend, with an onActionPerformed script, which allows a user to export/download all of the powerChart.props to a json file for future retrieval:

```auto
def runAction(self, event):
	# Extract Props from Power Chart object, encode as JSON:
	powerChart = self.view.getChild('root/PowerChart')
	propsJson = system.util.jsonEncode(powerChart.props)
	
	# Send file-download to client.
	filename = "myTrend.json"
	system.perspective.download(filename, propsJson)

```

1. Our solution for importing (somewhat lengthier-version of the export, by intention), involves a File-Upload component with the following onFileReceived script action:

```auto
def runAction(self, event):
	# Read file contents.
	fileName = event.file.name
	fileSize = event.file.size
	fileString = event.file.getString()
	
	# Convert file contents to JSON object.
	fileJSON = system.util.jsonDecode(fileString)
	
	# Define DateTime format from export (e.g. 'Jan 01, 2000, 11:59:59 PM')
	strDTFormat = 'MMM dd, yyyy, hh:mm:ss a'
	#"yyyy-MM-dd HH:mm:ss"
	
	def formatDateTime(wrkDateTime):
		if wrkDateTime is not None:
			return system.date.parse(wrkDateTime, strDTFormat)
		else:
			return wrkDateTime

	# Assign PowerChart object
	powerChart = self.view.getChild('root/PowerChart')
	
	# Write dictionary items from file to PowerChart Props:
	if 'config' in fileJSON:
		powerChart.props.config = fileJSON['config']
		# Convert all DateTime objects to format required of PowerChart:
		powerChart.props.config.startDate = formatDateTime(powerChart.props.config.startDate)
		powerChart.props.config.endDate = formatDateTime(powerChart.props.config.endDate)
		powerChart.props.config.rangeStartDate = formatDateTime(powerChart.props.config.rangeStartDate)
		powerChart.props.config.rangeEndDate = formatDateTime(powerChart.props.config.rangeEndDate)
	if 'interaction' in fileJSON:
		powerChart.props.interaction = fileJSON['interaction']
	if 'axes' in fileJSON:
		powerChart.props.axes = fileJSON['axes']
	if 'pens' in fileJSON:
		powerChart.props.pens = fileJSON['pens']
	if 'plots' in fileJSON:
		powerChart.props.plots = fileJSON['plots']
	if 'dataColumns' in fileJSON:
		powerChart.props.dataColumns = fileJSON['dataColumns']
	if 'title' in fileJSON:
		powerChart.props.title = fileJSON['title']
	if 'timeAxis' in fileJSON:
		powerChart.props.timeAxis = fileJSON['timeAxis']
	if 'legend' in fileJSON:
		powerChart.props.legend = fileJSON['legend']

```

Hope some of this helps!

---

<div class="post-metadata">

**Author:** ![Ryan\_Allen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_allen/32/47013_2.png) [@Ryan\_Allen](https://forum.inductiveautomation.com/u/Ryan_Allen)\
**Post date:** [April 5, 2023, 5:20pm UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/12 "2023-04-05T17:20:25Z")

</div>

Is it possible to automatically open a file with a button press using this method (or any method)? I'm trying to figure out how to setup pre-configured trends that the operator can load with the press of a button.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [April 5, 2023, 6:21pm UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/13 "2023-04-05T18:21:47Z")

</div>

Yes.

The Ad Hoc Trends project that @Chris_Bingham mentions, retrieves the saved config from a database, but that can be adapted to a file. The important part is the encoding and decoding to and from JSON.

---

<div class="post-metadata">

**Author:** ![Ryan\_Allen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_allen/32/47013_2.png) [@Ryan\_Allen](https://forum.inductiveautomation.com/u/Ryan_Allen)\
**Post date:** [April 5, 2023, 6:26pm UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/14 "2023-04-05T18:26:03Z")

</div>

Yeah I'm wondering how to automatically upload a specific file by pressing a button

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [April 5, 2023, 6:27pm UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/15 "2023-04-05T18:27:23Z")

</div>

Are you using Perspective or Vision? Were will the file be located, on a client machine, on the gateway, on a network drive?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 5, 2023, 6:27pm UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/16 "2023-04-05T18:27:49Z")

</div>

> [@Ryan\_Allen](#):
>
> automatically upload a specific file by pressing a button

Perspective can't do that. But a button could load a specific file from the gateway filesystem.

---

<div class="post-metadata">

**Author:** ![Ryan\_Allen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_allen/32/47013_2.png) [@Ryan\_Allen](https://forum.inductiveautomation.com/u/Ryan_Allen)\
**Post date:** [April 5, 2023, 6:36pm UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/17 "2023-04-05T18:36:15Z")

</div>

Thanks! Do you happen to have anything i can reference, like a function name, that would help me get this working?

---

<div class="post-metadata">

**Author:** ![Chris\_Bingham](https://avatars.discourse-cdn.com/v4/letter/c/e5b9ba/32.png) [@Chris\_Bingham](https://forum.inductiveautomation.com/u/Chris_Bingham)\
**Post date:** [April 6, 2023, 2:57pm UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/18 "2023-04-06T14:57:35Z")

</div>

If the goal is to preconfigure a chart on a button-press, you could start 'simple' by using a script to write the specific parameters you require to the chart directly. To aid in generating the script, you could start by generating the trend that you desire in Designer, then right-click on specific properties (for example, the entire 'props.pens' object) and copy to your clipboard. Then, modify the scripts aforementioned (except within a button's onActionPerformed\* event script) and paste the properties direct from your clipboard:

```auto
powerChart = self.view.getChild('root/PowerChart') # Update to match the path of your chart
powerChart.props.pens = (paste your pens JSON here)

```

You'll likely need to manipulate a few items so that they interpret correctly (true --\> True, etc.). Once you're able to preset a trend with this method from your designer, other clients should experience same results.

Depending on number of 'pre-built trends' required, I could see this method growing to be undesirable. At this point (say, more than...3 trends?), I would consider using a database (or file, or other means of lookup), whereby a dropdown menu could select from the list of trends.

---

<div class="post-metadata">

**Author:** ![Ryan\_Allen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_allen/32/47013_2.png) [@Ryan\_Allen](https://forum.inductiveautomation.com/u/Ryan_Allen)\
**Post date:** [April 7, 2023, 2:20pm UTC](https://forum.inductiveautomation.com/t/saving-a-configuration-of-a-power-chart-in-runtime/45840/19 "2023-04-07T14:20:47Z")

</div>

Thanks Chris, this is exactly what I ended up doing
