Is there any idea what he is doing in the below code at line
"chartSeries.AddItem(new ChartSeriesItem(listData[i].x, listData[i].y));"
I took this code from one forum.
http://www.telerik.com/community/forums/aspnet-ajax/chart/performance-problem-on-line-chart.aspx
I was not able to understand what kind of data holder it is?
Am pulling the data from in dataset format, If I could bind my dataset with listdata, I would acheive this development. But am not undestanding how I need to make this binding.
I have tried as below but didn't worked and throghing an error, please find the attached.
Dim listdata As new DataList
listdata.DataSource = ds.Tables.Item(0)
listdata.DataBind()
Original code took from forum
ChartSeries chartSeries = new ChartSeries("Altitude", ChartSeriesType.Line);
chartSeries.Appearance.ShowLabels = false;
incrementCount = 1;
for (int i = 0; i < listData.Count; i = i + incrementCount)
{
chartSeries.AddItem(new ChartSeriesItem(listData[i].x, listData[i].y));
}
RadChart2.AddChartSeries(chartSeries);
RadChart2.PlotArea.XAxis.AxisLabel.TextBlock.Text = "Distance (Miles)";
RadChart2.PlotArea.YAxis.AxisLabel.TextBlock.Text = "Yaxislabel";
I think its as simple as this, the error is that you can't use the word New, the code from the forum says to use new, capitalisation can make all the difference in .NET.
Related
Background: I'm trying to write a program to insert an image into a cell of a spreadsheet. LibreOffice recently changed how this is done, and all the samples I could find use the old method which no longer works.
Technically I know that you can't "insert" an image into a cell and that such an image is an overlay on a DrawPage that sits on top of the spreadsheet to "decorate" it.
One of the first steps in doing this (the new way) is to create an XGraphic object which contains the image. The process is to create an XGraphicProvider and call it with MediaProperties that specify the image file URL to be loaded. I have a program that is supposed to do this but the resulting XGraphic is null. The LO SDK gives pretty much no information when you do something wrong; it just doesn't work.
Here is the code I have, with all the headers removed:
// addpic
// add picture to spreadsheet - debug version
class OpenOfficeApp {
[STAThread]
static void Main(string[] args) {
bool lreadonly;
string pqfile;
string pqURL;
string pqpic;
pqfile = "file:///D:/Documents/NSexeye/ODS%20File%20Access/"+
"addpix/addpic.ods";
pqpic = "addpic2";
pqURL = pqpic+".jpg";
lreadonly = false;
Console.WriteLine("Using: "+pqfile);
// get the desktop
XComponentContext XCC = uno.util.Bootstrap.bootstrap();
XMultiComponentFactory XMCF =
(XMultiComponentFactory)XCC.getServiceManager();
XMultiServiceFactory XMSF = (XMultiServiceFactory)XCC.getServiceManager();
XComponentLoader XCL =
(XComponentLoader)XMSF.createInstance("com.sun.star.frame.Desktop");
// open the spreadsheet
PropertyValue[] pPV = new PropertyValue[2];
pPV[0] = new PropertyValue();
pPV[0].Name = "Hidden";
pPV[0].Value = new uno.Any(true);
pPV[1] = new PropertyValue();
pPV[1].Name = "ReadOnly";
if (lreadonly) pPV[1].Value = new uno.Any(true);
else pPV[1].Value = new uno.Any(false);
XComponent XCo = XCL.loadComponentFromURL(pqfile,"_blank",0,pPV);
// create graphic object containing image
object oGP = XMCF.createInstanceWithContext(
"com.sun.star.graphic.GraphicProvider",XCC);
if (oGP == null) {
Console.WriteLine("oGP is null. Aborting.");
return;
}
XGraphicProvider XGP = (XGraphicProvider)oGP;
if (XGP == null) {
Console.WriteLine("XGP is null. Aborting.");
return;
}
pPV = new PropertyValue[1];
pPV[0] = new PropertyValue();
pPV[0].Name = "URL";
pPV[0].Value = new uno.Any(pqURL);
Console.WriteLine("Creating XGraphic containing "+pqURL);
XGraphic XG = XGP.queryGraphic(pPV);
// *** XG is null here
if (XG == null) {
Console.WriteLine("XG is null. Aborting.");
return;
}
// ... lots of stuff to be added here
// save and close the spreadsheet
XModifiable XM = (XModifiable)XCo;
XM.setModified(true);
XStorable XSt = (XStorable)XCo;
XSt.store();
XCloseable XCl = (XCloseable)XCo;
XCl.close(true);
// terminate LibreOffice
// *** I want this to not terminate it if something else is open
XDesktop XD = (XDesktop)XCL;
if (XD != null) XD.terminate();
}
}
I get a null for the XGraphic, in the place indicated in the comments. I don't know if the call to create it is failing, or if one of the earlier steps of the process are incorrect.
My goal here, in addition to getting my program working, is to create a sample program showing how to add an image to a Calc spreadsheet cell, and to manipulate such images. There are a fair number of people asking questions about this and none of the examples I've found will work. I think a good working sample will be of value.
I've spent a lot of time searching for information and code samples for this, with nothing that helps. I've tried to find ways to verify the validity of the XGraphicProvider interface with no luck. I've run out of things to try.
I'm hoping someone who knows about the LibreOffice SDK can take a look and maybe see what I'm doing wrong.
Update: I figured out what I was doing wrong: I was passing a bare filename in the "URL" property to XGraphicProvider. It has to be the same format (starting with "file:///") as the spreadsheet's file name specification.
Now I'm stuck with another property problem. The XGraphic has to be specified as a parameter to the GraphicObjectShape's Graphic property, but the setPropertyValue() function requires that it be a uno.Any type. I can't figure out how to specify an interface name like XGraphic as a uno.Any.
Here is the piece of code that won't compile, complaining that it can't convert an XGraphic to a uno.Any, in the first setPropertyValue call:
// set image XGraphic
XPropertySet XPS = (XPropertySet)XS;
XPS.setPropertyValue("Graphic",XG);
XPS.setPropertyValue("Name",new uno.Any(pqpic));
XG is an XGraphic type. Using "new uno.Any(XG)" doesn't work either, giving a similar compiler error.
After trying unsuccessfully for a few hours to get the latest LO SDK up and running, let me offer some untested ideas.
First of all, here is some working Basic code, no doubt similar to what you're translating from. The important line is oShape.Graphic = oProvider.queryGraphic(Props()).
oDoc = ThisComponent
oSheet = oDoc.CurrentController.ActiveSheet
pqURL = "file:///C:/Users/JimK/Desktop/addpic.jpg"
oProvider = createUnoService("com.sun.star.graphic.GraphicProvider")
oShape = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
Dim Props(0) as new com.sun.star.beans.PropertyValue
Props(0).Name= "URL"
Props(0).Value = pqURL
oShape.Graphic = oProvider.queryGraphic(Props())
oCell = oSheet.getCellByPosition(5,5)
oShape.Name = oCell.AbsoluteName + "##" + Props(0).Value
oShape.Anchor = oCell
oSheet.DrawPage.add(oShape)
'Resize
w = oShape.Graphic.Size.Width
h = oShape.Graphic.Size.Height
wcl = oCell.Size.Width
hcl = oCell.Size.Height
If w<>0 and h<>0 then
oCell.String=""
Dim Size as new com.sun.star.awt.Size
Size.Width = wcl
Size.Height = h*wcl/w
If Size.Height > hcl then
Size.Width = hcl*w/h
Size.Height = hcl
Endif
oShape.setSize(Size)
oShape.setPosition(oCell.Position)
erase oShape
Else
oShape.dispose()
Endif
Now, how to translate this to C#? It looks like you may need to explicitly specify the type. In the SDK example, there are calls like this.
xFieldProp.setPropertyValue(
"Orientation",
new uno.Any(
typeof (unoidl.com.sun.star.sheet.DataPilotFieldOrientation),
unoidl.com.sun.star.sheet.DataPilotFieldOrientation.DATA ) );
So in your case, something like this:
XPS.setPropertyValue(
"Graphic"
new uno.Any(
typeof(unoidl.com.sun.star.graphic.XGraphic),
XG));
Alternatively, follow the suggestion here: set GraphicURL, which should load the image and set Graphic for you.
Before anyone points it out, yes, I know there have been similar questions asked before. I've already tried solutions from them and they do not work, which is why I'm asking this question.
For reference, I'm using Android API 30.
So I'm performing a query on all audio files on the device using MediaStore. As of now I'm able to properly access artist/album/song IDs, names, etc. and use them elsewhere in my app. The one thing I'm unable to get though is the album art.
This is what my code looks like:
string[] columns = {
MediaStore.Audio.Media.InterfaceConsts.IsMusic,
MediaStore.Audio.Media.InterfaceConsts.Data,
MediaStore.Audio.Media.InterfaceConsts.Title,
MediaStore.Audio.Media.InterfaceConsts.CdTrackNumber,
MediaStore.Audio.Media.InterfaceConsts.Duration,
MediaStore.Audio.Media.InterfaceConsts.Id,
MediaStore.Audio.Media.InterfaceConsts.Album,
MediaStore.Audio.Media.InterfaceConsts.AlbumId,
MediaStore.Audio.Media.InterfaceConsts.Artist,
MediaStore.Audio.Media.InterfaceConsts.ArtistId,
};
ICursor? cursor = context.ContentResolver?.Query(MediaStore.Audio.Media.ExternalContentUri!, columns, null, null, null);
if (cursor is null)
{
return;
}
while (cursor.MoveToNext())
{
if (cursor.GetString(0) is not "1")
{
continue;
}
string trackPath = cursor.GetString(1)!;
string trackTitle = cursor.GetString(2)!;
int trackIndex = cursor.GetInt(3);
uint trackDuration = (uint)(cursor.GetFloat(4) / 1000);
long trackId = cursor.GetLong(5);
string albumTitle = cursor.GetString(6)!;
long albumId = cursor.GetLong(7);
string artistName = cursor.GetString(8)!;
long artistId = cursor.GetLong(9);
I initially tried the obvious method, which was adding MediaStore.Audio.Media.InterfaceConsts.AlbumArt to columns and getting the data from that column. But just adding that causes the application to freeze. Logging shows that adding it causes the method to freeze midway and not do anything. So this solution is out.
I then tried MediaMetadataRetriever, like this:
MediaMetadataRetriever retriever = new();
retriever.SetDataSource(trackPath);
byte[] artwork = retriever.GetEmbeddedPicture()!;
Bitmap albumArt = BitmapFactory.DecodeByteArray(artwork, 0, artwork.Length)!;
However, this also fails. I get an error message in the logs saying that MediaMetadataRetriever was unable to set the data source to that source.
So I figured maybe my data source was wrong. After doing some digging around I tried using a different path:
Uri contentUri = Uri.Parse("content://media/external/audio/albumart")!;
Uri albumArtUri = ContentUris.WithAppendedId(contentUri, albumId);
MediaMetadataRetriever retriever = new();
retriever.SetDataSource(albumArtUri.Path);
...
This also does not work. Neither does using MediaStore.Audio.Albums.ExternalContentUri.
I even tried opening a ParcelFileDescriptor from those URIs. Also doesn't work.
Does anyone know of a way that would definitely work? Most of the answers on StackOverflow seem to be quite dated, so they possibly don't apply to API 30 anymore. But I don't know what I'm doing wrong since the documentation isn't very detailed.
Im still working on my first asp.net core project and now I want to display "a qoute of the day".
I have the qoutes in a xml file stored in a folder called File under wwwroot.
Im planning on my making this a View Component.
Im used to working with web forms so it seems like Im spending alot of time on small issues, but I guess its the only way to learn.
I've created a folder named Custom where I plan to hold all my custom classes. the QuoteController.cs is located in the Controllers folder.
So yeah, I think I know how to crate the View Component. "I think" is an important factor here.
Im also used to using XmlDocument, so Im trying my best to get XmlReader to work. But any hint or tips would be highly appreciated.
This is what I got so far. QuoteController.cs
public class QuoteController : Controller
{
public Custom.Quote Index()
{
Custom.Quote result = new Custom.Quote();
XmlReader rdr = XmlReader.Create(#"\File\qoutes.xml");
Random rnd = new Random(DateTime.Now.Millisecond);
int tmp = rdr.AttributeCount;
int count = rnd.Next(0, tmp);
int i = 0;
while (rdr.Read())
{
if (count.Equals(i))
{
result = new Custom.Quote(rdr.GetAttribute("q"), rdr.GetAttribute("author"));
break;
}
i++;
}
rdr.Dispose();
rdr = null;
rnd = null;
return result;
}
}
I guess the next step will be to add some visuals, but I cant imagine that my code actully works. Does anybody know how to easily parse through and xml file i CORE? Should I go for async?
I guess it doesnt matter, but the xml file is formated like:
<quotes>
<q>Be Strong</b>
<author>Stein The Ruler</author>
</quotes>
Again, I will be very happy if you take the time to look at this :)
Thank you!
My way to implement this:
1)convert the xmldocument to look like this
<quotes>
<quote Content="Be Strong" Author="Stein..."/>
</quotes>
2) Fix the Custom.Quote object to contain these 2 (public getters, setters string) fields: Content and Author,
and then,3) use this code to turn the xml to a list:
XDocument quotesDoc = XDocument.Parse('your path');
List<Custom.Quote> quotes = quotesDoc.Root
.Elements("quote")
.Select(x => new Speaker
{
Content= (string)x.Attribute("Content"),
Author = (string)x.Attribute("Author")
})
.ToList<Custom.Quote>();
Hope this helps!
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
I am using C# and using the YouTube V3 API. I am trying to insert a comment into a Video however whenever I do I receive a exception of "{"Object reference not set to an instance of an object."}" This happens whenever I run anything similar to the above code:
public void AddComment()
{
CommentThread commentToAdd = new CommentThread();
commentToAdd.Snippet.IsPublic = true;
commentToAdd.Snippet.TopLevelComment.Snippet.TextOriginal = "Test";
commentToAdd.Snippet.VideoId = "kc-LBxBcyG8";
commentToAdd.Snippet.TopLevelComment.Snippet.VideoId = "kc-LBxBcyG8";
CommentThreadsResource.InsertRequest ins = JKYouTube.NewYouTubeService().CommentThreads.Insert(commentToAdd, "snippet");
var insertedComment = ins.Execute();
}
I am comparing this to the google explorer and using the same properties and the explorer actually adds the comments where as my program just fails. https://developers.google.com/youtube/v3/docs/commentThreads/insert
As soon as it reaches the second line of code of
commentToAdd.Snippet.IsPublic = true;
It will just error and continue for every line above.
Any help would be greatly appreciated.
Your problem lies in the fact Snippet is null.
Taken from the API link that you have given, you need to first create a CommentSnippet.
In the example that Google has provided:
// Insert channel comment by omitting videoId.
// Create a comment snippet with text.
CommentSnippet commentSnippet = new CommentSnippet();
commentSnippet.setTextOriginal(text);
First, a CommentSnippet is created with some text and then we create a top level comment:
// Create a top-level comment with snippet.
Comment topLevelComment = new Comment();
topLevelComment.setSnippet(commentSnippet);
And then, you add your topLevelComment to the CommentThreadSnippet:
// Create a comment thread snippet with channelId and top-level
// comment.
CommentThreadSnippet commentThreadSnippet = new CommentThreadSnippet();
commentThreadSnippet.setChannelId(channelId);
commentThreadSnippet.setTopLevelComment(topLevelComment);
When finally you have your CommentThreadSnippet, you can add it to a CommentThread:
// Create a comment thread with snippet.
CommentThread commentThread = new CommentThread();
commentThread.setSnippet(commentThreadSnippet);
Following those steps should not give you a NRE
Many thanks for your help. Managed to complete it.
async Task AddVideoCommentAsync(string commentToAdd, string videoID)
{
CommentSnippet commentSnippet = new CommentSnippet();
commentSnippet.TextOriginal = commentToAdd;
Comment topLevelComment = new Comment();
topLevelComment.Snippet = commentSnippet;
CommentThreadSnippet commentThreadSnippet = new CommentThreadSnippet();
commentThreadSnippet.VideoId = videoID;
commentThreadSnippet.TopLevelComment = topLevelComment;
CommentThread commentThread = new CommentThread();
commentThread.Snippet = commentThreadSnippet;
var youtubeService = await NewYouTubeService();
CommentThreadsResource.InsertRequest insertComment = youtubeService.CommentThreads.Insert(commentThread, "snippet");
await insertComment.ExecuteAsync();
}
I have all the necessary requirements when using the R.NET from http://rdotnet.codeplex.com/
My code works just fine on R Studio, however no luck on GUI. Can anybody let me know what I am doing wrong please?
REngine.SetEnvironmentVariables(#"C:\Program Files\R\R-3.1.1\bin\i386", #"C:\Program Files\R\R-3.1.1");
engine = REngine.GetInstance();
engine.Evaluate(#"source('C:/Users/achugh/Documents/Graphs/characterization.r')");
engine.Evaluate(#"source('C:/Users/achugh/Documents/Graphs/sliderDataToComputer.r')");
var sliderfunc = engine.Evaluate("sliderdata_yprofile").AsFunction();
var directory = engine.CreateCharacterVector(new[] { "C:/Users/achugh/Documents/Graphs/data" });
var oldset = sliderfunc.Invoke(new SymbolicExpression[] { directory }).AsDataFrame();
But for some reason the 'oldset' always evaluates to NULL. I already tried testing this via R-Studio
please advice?
Are you absolutely sure your function returns a data frame and not a matrix? The following behaves exactly as expected, and as you describe. I am working from the latest code but this part of R.NET is identical to the latest 1.5.16. Please mark this post as an answer if indeed correct, just not to confuse readers as to the behavior of R data coercion.
var funcDef = #"function(lyrics) {return(data.frame(a=1:4, b=5:8))}";
var f = engine.Evaluate(funcDef).AsFunction();
var x = f.Invoke(engine.CreateCharacter("Wo willst du hin?"));
Assert.True(x.IsDataFrame());
Assert.True(x.IsList());
var df = x.AsDataFrame();
Assert.NotNull(df);
funcDef = #"function() {return(as.matrix(data.frame(a=1:4, b=5:8)))}";
f = engine.Evaluate(funcDef).AsFunction();
x = f.Invoke();
Assert.False(x.IsDataFrame());
Assert.False(x.IsList());
df = x.AsDataFrame();
Assert.Null(df);
var nm = x.AsNumericMatrix();
Assert.NotNull(nm);
Answer:
var oldset = sliderfunc.Invoke(new SymbolicExpression[] { directory }).AsDataFrame();
change the above line to :
var oldset = sliderfunc.Invoke(new SymbolicExpression[] { directory }).AsNumericMatrix();
The reason are unknown, although the script is returning a data frame , but it fails to recognize this as data frame but recognizes this as Numeric Matrix.