Passing a string from .NET to a javascript variable - javascript

I have tried many ways to pass a string from .net to javascript including .NET serializer, and register startup scripts.
I am looking for the simplest way to populate a javascript variable with a string generated in code behind.
This is what I am currently trying but output is ""
I am open to other suggestions.
VB.NET
Public Property ServerVariable As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim ServerVariable = "'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'"
End Sub
JAVASCRIPT
var data = "<%= ServerVariable %>";
...
List : [data]
DESIRED RESULT
List : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

This should be a comment, sorry, btw you can access to public props in the aspx;ascx, so this should help
vb.net
Public Property ServerVariable As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
ServerVariable = "['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']"
End Sub
JS
var data = "<%= ServerVariable %>";

You are doing it correctly, only thing is that you need to do .split(",") in javascript to get desired result.
Like var list = console.log(data.split(","));

Missing get and set values. Passed variable ServerVariable needs to have a value applied to it.
Private MyVar As String = ""
Public Property ServerVariable As String
Get
Return MyVar
End Get
Set(value As String)
MyVar = value
End Set
End Property
MyVar = "['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']"

Related

Invoke function from div with angularJS parameter

Below I'm attempting to convert millisecond to javascript date :
<div ng-app>
<div ng-controller="TodoCtrl">
<div>
<script type="text/javascript">
parseDate({{test}})
</script>
</div>
</div>
</div>
function TodoCtrl($scope) {
$scope.test = "1429831430363"
}
function parseDate(date) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return months[date.getUTCMonth()] + ' ' + date.getUTCDate();
}
http://jsfiddle.net/U3pVM/15141/
But error Uncaught SyntaxError: Unexpected token { is thrown on console.
What is correct method of invoking function from div with angularJS parameter ?
use this code : see fiddle
<div ng-app>
<div ng-controller="TodoCtrl">
<div>
{{parseDate(test)}}
</div>
</div>
</div>
function TodoCtrl($scope) {
$scope.test = "1429831430363"
$scope.parseDate=function(date) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return months[(new Date()).getUTCMonth()] + ' ' + (new Date()).getUTCDate();
}
}
A more elegant solution to format a date is creating a custom filter because is more clean and you can re-use it everywhere:
Filter:
myApp.filter('myMillisecondsToUTCDate', [function() {
return function(milliseconds) {
var date = new Date(milliseconds);
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return months[date.getUTCMonth()] + ' ' + date.getUTCDate();
};
}]);
Controller:
myApp.controller('MyCtrl', function($scope, $filter) {
$scope.testDate = 1429831430363;
// Re-Use the filter everywhere
$scope.formattedDate = $filter('myMillisecondsToUTCDate')($scope.testDate);
});
Html:
<div ng-controller="MyCtrl">
Format a date using a custom filter: {{testDate | myMillisecondsToUTCDate}}<br/>
Formatted date {{formattedDate}}
</div>
Check the demo fiddle
You can use angular.element and jqLite.scope to get the scope object. Remember that angular and your app need to be initialized for this to work, so you'd better call parseDate in a callback.
parseDate(
angular
.element('[ng-controller="TodoCtrl"]') // but you'd rather give it an ID / class name / whatever
.scope()
.test
);
Modififying your code:
We dont need script inside divs as they are still evaluated seperately.
defined parseDate inside angularjs controller, If this is really general, define it as a service/factory and use it everywhere. That is the angular way.
Your test is a string. It doesnot have getUTCMonth function, it should be a number so that it can be converted to a date so that we can use the above function.
following is the modified code, which works:
<div ng-app>
<div ng-controller="TodoCtrl">
<div>
{{parseDate(test)}}
</div>
</div>
</div>
And the controller:
function TodoCtrl($scope) {
$scope.test = 1429831430363;
$scope.parseDate = function(date) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
date = new Date(date);
return months[date.getUTCMonth()] + ' ' + date.getUTCDate();
}
}
See this fiddle: http://jsfiddle.net/U3pVM/15146/

Remove first and last grid lines in highcharts?

I want to separate the points on a line graph by vertical lines in Highcharts. I can do this with grid lines, but I would like the lines to display only between points and not before the first point and after the last one. In the fiddle there should be no grid lines before 'Jan' and after 'Dec'. Is it possible?
Here is my x-axis
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
gridLineWidth: 1,
minorGridLineWidth: 0
}
Just delete two last <path> elements in the group <g> element .
Example on fiddle
Axis has classname attribute.
set an axis classname.
set CSS to hide the last path element.
.axis-container {
path:last-child {
display: none;
}
}

AngularJS how can select value from ng-options

i am inserting value in select ng-options and i want to select in dropdown for particular month.
this is return all 12 month arrray, so how can i fetch only selected month from dropdown.
Script:
$scope.Month = ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
$scope.selectmonth = $scope.Month;
$scope.SaveReport = function () {
PrintReportRepository.save(
{
SelectMonth: $scope.selectmonth,
},
html
<select class="form-control" ng-model="selectmonth" ng-options ="months as months for months in Month">{{months}}</select>
For setting first item selected use $scope.selectmonth = $scope.Month[0], by setting $scope.selectmonth = $scope.Month U setting whole array as a model
use this code: see plunker
controller:
$scope.Month = ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
$scope.SaveReport = function () {
console.log('SelectMonth', $scope.selectmonth);
}
html:
<select class="form-control" ng-model="selectmonth" ng-options ="months as months for months in Month" ng-change="SaveReport()"></select>

Highcharts min y-axis value

Is it possible to set a min y-axis value when the series values are the same?
For example:
http://jsfiddle.net/6hyfk/1/
I would like to see 0.00019 as y-axis value.
If i change it to:
series: [{
data: [0.00019,0.00020,0.00019,0.00019]
}]
it looks a lot better.
So the answer largely depends on your data. If for example, you know your values are all positive, setting a yAxis min helps your data display a bit better. You know your data better than I, so I'd look at the yAxis settings and see what you can change to get a better result, but for example, this is a JSFiddle that sets a minimum value:
$(function () {
$('#container').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
tickLength: 20
},
yAxis: {
min: 0
},
series: [{
data: [0.00019,0.00019,0.00019,0.00019]
}]
});
});
http://jsfiddle.net/gTG2z/
You can manually set the min and max on the X axis.
yAxis:{
min: 0,
max: .001
},
http://jsfiddle.net/6hyfk/2/
If you know what range is in your series, you can set accordingly minRange, so Highcharts will render more ticks as expected. For example: http://jsfiddle.net/Fusher/6hyfk/3/ Good thing about minRange is that it will work also for another values like that: http://jsfiddle.net/Fusher/6hyfk/5/
Probably,you don't need to hardcode min,max values.Do calculation for min and max and set to y Axis as shown below,
$(function () {
yMin = Math.min.apply(Math,data); // Find Min
yMax = Math.max.apply(Math,data); //Find Max
$('#container').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
min: yMin,
max: yMax
},
series: [{
data: data //Array of values
}]
});
});

highchart: add images to top of chart on every column

Does anyone know if it's possible to create something like this in Highcharts:
It's about the weather icons on the top. I added them as a "scatter graph" which is nice, so the images/graph can be disabled. But I want them always at the top. For example: y=20px or something. Is it possible to do this with Highchart? I know set their data to "30 celcius" but that would mess up the graph if it the temperature would go up to 30 degrees.
You can use a trick of having two x-axes, one with images and offset'ed to the top of the chart and one with the usual labels at the bottom:
xAxis: [{
offset: -290,
tickWidth: 0,
lineWidth: 0,
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
labels: {
x: 5,
useHTML: true,
formatter: function () {
return '<img src="http://highcharts.com/demo/gfx/sun.png"><img> ';
}
}
}, {
linkedTo: 0,
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
}],
Full example on jsfiddle
I found a solution by chance because of a logging code I forgot to remove. You can access the data inside the method formatter using "this":
...
plotOptions: {
series: {
dataLabels: {
useHTML: true,
enabled: true,
x: -50,
y: -50,
formatter: function(){
console.log(this);
return '<span>'+this.y+'</span><br/>'+this.series+'<img src="'+this.key+'" />';
},
}
...
This code surely isn't working, but shows up the idea, doesn't it?
Hope it helps!
Given that highcharts is just SVG you can always go directly and manipulate the SVG to show the images you want, usually with just CSS. You can inspect the chart with Chrome's web inspector and find the elements you want to style. I have to warn you though that having customized highcharts myself in the past has made upgrading to newer versions difficult.
You can add another scatter plot to your series that contains specific markers: http://jsfiddle.net/ZZ7Kd/135/
You can use Renderer.image() to add images in any place on chart
http://api.highcharts.com/highcharts#Renderer.image()

Categories