I've been working with javascript Highcharts and I made a basic 'Chart Builder' app. One of my goals is to have the user create and modify as many options as they like and save those to the db. The main problem I'm having is trying to convert the Highcharts object to a c# class. I've been building it slowly(ie manually) with the parts I need, as I need them, but to eventually get the whole thing converted will take a long time.
Ideally, I'd like to create and setup the whole highcharts options object server side and just send it 100% complete to highcharts
Is there any easy way to do this? Has anyone already done this?
Here is the Highcharts reference page: http://www.highcharts.com/ref/
and this is what I've done so far.
public class Highchart
{
public title title { get; set; }
public plotOptions plotOptions { get; set; }
}
public class title
{
public string text { get; set; }
}
public class plotOptions
{
public series series { get; set; }
}
public class series
{
public string stacking { get; set; }
public string borderColor { get; set; }
public bool shadow { get; set; }
public int borderWidth { get; set; }
}
As you can see, I just started ^_^
Update : The Highcharts .Net library has been updated in December, and is nearly feature complete as per V2.1.9 of the Javascript library.
The .Net library currently has support for multiple axes, point objects, viewstate management after postbacks, click events for points, series etc, and a built in implementation of an AJAX datasource ;) You don't need to write a single line of JS code unless you want to handle click events; you simply code in C#, and the appropriate JS is rendered automatically for you..
Click here to view the Live Demo
Related
I am new to .net core - have been using aspx web pages and .net framework 4.x for a number of years. I have a project where we want to display different controls (textbox, dropdown, checkbox) on the page based on values returned from a query. For example, user chooses "A" from a dropdown list and it shows 10 controls, if they choose object B it shows 8 controls, etc. Previously in .net framework, I would use a content placeholder with an ID and then find that ID and start adding controls (controls.Add(newControl)) in the placeholder. It doesn't seem that is an option with .net core. It seems like this would be a common need for various web applications, but I'm not finding many hits.
Another question is whether this can be done in the code behind or if it has to be done on the client-side. If one of the controls in the list is a dropdown, there will be a query that a subroutine will run to get the Key/Value pairs for the dropdown. To me this means it would be more effective on the server side.
I haven't really found any good examples when I do some searching. Can anyone point me to a good resource or provide me with a basic example - either client-side or server-side? Thanks!
There are many options, but I'll describe a simple one, using server side processing. As you explained in your comment, there will be 2 pages:
One that will display the select element that will be used to choose a set of controls.
The page that will be returned according to the previous choise, displaying the selected set of controls.
I assume that you know how to build the first page.
For the second page, you can leverage the ASP.NET Core MVC pattern to achieve the desired result.
You will need the three usual MVC elements:
An Action in a Controler.
A ViewModel for your Razor View.
A Razor View.
The Action does the following:
Receives the id of the selected set of control (via the Action's parameter).
Uses this id to retrieve the information about the corresponding set of controls from your repository.
Builds a ViewModel out of the received information.
Builds a View using the obtained ViewModel.
Return the builded View.
Here is some simplified example code:
In your controller, add the following method:
#!lang-cs
Public IActionResult GetProgramControlSet(int ProgramId)
{
// Here, use the id to get the data from your repository
// that will be used to build set of controls.
// Supposing you have defined a GetControls method,
// it could look like:
var SelectedControls = MyRepository.GetControls(ProgramId);
// If needed, you can build a ViewModel out of the received SelectedControls.
var SelectedControlsViewModel = new ControlSetViewModel(SelectedControls);
return View(SelectedControlsViewModel)
}
Of course, many things are missing here: error handling, etc...
Here is what the ViewModel could be:
#!lang-cs
public class ControlSetViewModel
{
public string Name { get; private set; }
public List<IControl> Controls { get; private set; }
public ControlSetViewModel(...)
{
// Whatever needs to be done to construct the ViewModel
}
}
public enum ControlKind
{
Button,
Select,
Textarea
//...
}
public interface IControl
{
ControlKind Kind { get; }
}
public class ControlButton : IControl
{
public ControlKind Kind => ControlKind.Button;
public string Label { get; set; }
public string Text { get; set; }
public string Color { get; set; }
// ... All other needed properties for the button
}
public class ControlTextarea : IControl
{
public ControlKind Kind => ControlKind.Textarea;
public string Label { get; set; }
public string PlaceholderText { get; set; }
public string RowCount { get; set; }
// ... All other needed properties for the textarea
}
public class ControlSelect : IControl
{
public ControlKind Kind => ControlKind.Select;
public string Label { get; set; }
public string PlaceholderText { get; set; }
public List<SelectOption> Options { get; set; }
// ... All other needed properties for the select
}
public class SelectOption
{
public string Text { get; set; }
public string Value { get; set; }
}
You could also use inheritance instead of interface for the control classes.
Now the view.
It is a Razor page containing something akin to
#model ControlSetViewModel
#*... some HTML ...*#
<div>
<h1>#Model.Name</h1>
#foreach(var control in Model.Controls)
{
<div>
switch(control.GetControlKind())
{
case ControlKind.TextArea:
var Textarea = (ControlTextarea)control;
<label>#Textarea.Label</label>
<textarea rows="#Textarea.RowCount"/>
break;
case ControlKind.Select:
var Select = (ControlSelect)control;
<label>#Select.Label</label>
<select>
#foreach(var option in Select.Options)
{
<option value="#option.Value">#option.Text</option>
}
</select>
break;
#*... etc ...*#
default:
#*... etc ...*#
}
</div>
}
</div>
#*... More HTML ...*#
Of course this is far to be finished. All the infrastructure and code that will actually react to the displayed controls is missing.
Is it a form you that will be posted?
Is it Javascript code that will react to the control manipulation?
Or another mecanism?
This questions will need to be addressed.
I'm currently developing a system which manages work times and analyze them. Therefore I need to add several Time-Stamps once and add/remove some input field dynamically.
The ViewModel looks like:
public class CreateWorkDayViewModel
{
public DateTime Date { get; set; }
public IEnumerable<CreateStampModel> Stamps { get; set; }
}
public class CreateStampModel
{
public string ProjectId { get; set; }
public DateTime From { get; set; }
public DateTime To { get; set; }
}
How to do this with the razer syntax including validation?
I'm developing with Visual Studio Version 15.2 and the latest stable MVC version.
EDIT (17.05.17):
What I've done so far:
I added a jQuery based mechanism to add/remove rows dynamically.
Click for the mechanism
My problem is, that in the part of where I'm adding the rows(at JS line 4) I can't use the razor syntax because this html code is inserted at runtime by jQuery.
I also tried it with the old way and used instead of asp-for name. This works for the post data, but to fill the project drop-down selection I need the data from the ViewModel and this is missing.
In my front end, I'm trying to call methods dynamically, but I keep getting errors of contexts.
My code looks like this and the error is given by my "i":
.aspx :
<script src="https://PCYULD0029:8012/Maps/leaflet.js"></script>
<script >
... some code
AllObject = "<%=(ParseMapObjects())%>";
L.marker([0, 0], 0).addTo(map).bindPopup("test");
for (var i = 0; i < 2; i++)
{
ObjLongitude = AllObject[i].Longitude;
ObjLatitue = AllObject[i].Latitude;
CreateMarkers(ObjLongitude, ObjLatitude)
L.marker([0, 0], 0).addTo(map).bindPopup("test");
L.marker([0, 50], 0).addTo(map).bindPopup(ObjLatitue);
}
function CreateMarkers(ObjLong, ObjLat) {
L.marker([ObjLong, ObjLat], 0).addTo(map).bindPopup("Test");
}
... some code
</script>
.aspx.cs :
public List<MapObjectEntity> ParseMapObjects()
{
List<MapObjectEntity> MapObjects = new List<MapObjectEntity>();
.... Some code
return MapObjects;
}
public class MapObjectEntity
{
// Properties
public Guid MapObjectGuid { get; set; }
public string Data { get; set; }
public int Latitude { get; set; }
public Guid Link { get; set; }
public int Longitude { get; set; }
public int RelativeHeight { get; set; }
public int RelativeWidth { get; set; }
public int Rotation { get; set; }
public bool ObjectShowFov { get; set; }
public Guid MapObjectType { get; set; }
}
So, in ParseMapObjects, I return a List of MapObjectEntity. I then try to catch that in my .aspx (frontend), so that I can use it as an object (ex: Object.property).
With the line AllObject = "<%=(ParseMapObjects())%>"; I succeed at pulling the object, but I can't seem to find a way to use AllObject to get my properties (ex: AllObject[0].Latitude).
My objective here is to loop through my AllObject and extract all the Latitude and Longitude values. But I can
thank you
Unless ParseMapObjects() returns a JSON string (something like caner's answer)
this won't work.
And bear in mind, once the page has loaded, this value will be fixed, since it's the result of the C# method that is output to the page, not a reference to the method. If you run the Javascript multiple times, the value assigned to AllObject on that line will stay fixed, because it's basically a static object in the JS context (check the View Source of your browser to see what is output).
You can't call a C# method directly from JavaScript, and vice versa. C# executes on the server and constructs the HTML, CSS and JS to output to the browser. JavaScript runs in the client's browser after the page (i.e. the content generated by the C#) has been downloaded from the server to the browser and initialised. They are in totally separate environments (usually on different computers) and intrinsically have no knowledge of each other. This separation is a vital concept to grasp if you want to develop web applications.
If you want communication between the client (browser) and the server outside of the normal page load / refresh / postback mechanism, then you need to use AJAX functionality, or even, if you want real-time communication, something like WebSockets and/or SignalR.
You need serialization...
public string ParseMapObjects()
{
List<MapObjectEntity> MapObjects = new List<MapObjectEntity>();
return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(MapObjects);
}
and access in your script like
var AllObject = <%=(ParseMapObjects())%>;
I got a graph from which I need to set some propery in objects. Im adding an example on which I will explain it better:
Assuming I have the following class:
public class Person
{
public int Account { get; set; }
public string BirthCity { get; set; }
public string Name { get; set; }
public Family Family { get; set; }
}
Each Person that gets to the DAL will automatically be assigned with Status according to that algorythm. My real problem is much more complex, but this example does explain it well I think.
The graph describes scenarios and I need to translate it to code. I want my solution to be as flexible to changes as possible. Ofcourse writing ifs and switch case is the easiest yet its not a good solution.
One idea I had was creating an Xml file suting the scenarios, but I think that it might not be that good.
Does anyone have any Ideas about this issue?
I'm still learning C# .Net 4 and this is my first WinForms so please be kind.
Continuing in my project, my financial DataFeed is streaming into my application by use of 'Asynchronous Sockets?'. Anyway, the data I am getting is tick per tick data, which is basically 1 order/transaction. So now I need to build bars with this tick by tick data, in particular Range Bars.
My problem is I don't want to go to the database and grab this data, so I am looking to do this in memory, like a list variable. Eventually, this system on the main server will do all the number crunching etc... and will have clients connected via Sockets to interrogate or set their own predefined algos on the in coming data and build their own charts using different ranges and indicators.
I wouldn't want to offload this to the client because I would like to keep the indicators technology proprietary.
How would I go about implementing this?
I already have my class called Tick
class Tick
{
public double Last { get; set; }
public double Bid { get; set; }
public double Ask { get; set; }
public double BidSize { get; set; }
public double AskSize { get; set; }
public DateTime TimeStampInternal { get; set; }
public int DTNTickID { get; set; }
public int UpdateTypeID { get; set; }
}
I'm thinking of a
Static List<Tick> Ticks
but I don't think this is the way to go because
I need to be able to hold only a certain amount of ticks and as new data comes in, old data gets thrown away, FIFO to keep memory usage down.
I will only be able to hold 1 Static List and I need something dynamic, e.g. have a List for each user that connects which would be identifiable to them only.
Please help me architect this correctly with best practices for speed and efficiency.
Sounds like a circular buffer is what you're looking for.
http://circularbuffer.codeplex.com/
Or perhaps a queue.
I hope that I correctly understand what you want, so here is very pseudocode :
public class User {
private UserTickList<Tick> _userTicks = new UserTickList<Tick>();
public void AddUserTick(Tick t) {
_userTicks.Add(t);
}
/*remove, update if need*/
}
public class UserTickList {
private List<Tick> _list = new List<Tick>();
public void AddTick(Tick tick) {
if(_list.Count == 10){
/*perform what you need*/
}
else
_list.Add(tick);
}
}
I repeat this probabbly will not compile, but just to give an idea what it can look like.
Hope this helps.